From f28aa748f179958cd61a3d6bc2011f0bc4329d32 Mon Sep 17 00:00:00 2001 From: ajay-dhangar Date: Wed, 29 May 2024 22:31:06 +0530 Subject: [PATCH] resolved and write in better way for dsa solution --- .../lc-solutions/0000-0099/0001-two-sum.md | 86 +++++++++++++------ 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md b/dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md index 783c97002..eda15b837 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md +++ b/dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md @@ -15,8 +15,8 @@ description: "This is a solution to the Two Sum problem on LeetCode." ## Problem Description -|Problem Statement | Solution Link | LeetCode Profile| -|:---|:---|:---| +| Problem Statement | Solution Link | LeetCode Profile | +| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- | | [Two Sum on LeetCode](https://leetcode.com/problems/two-sum/) | [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions) | [Ajay Dhangar](https://leetcode.com/ajaydhangar49/) | ## Problem Description @@ -34,7 +34,7 @@ You can return the answer in any order. ```plaintext Input: nums = [2,7,11,15], target = 9 Output: [0,1] -``` +``` **`Example 2:`** @@ -67,7 +67,9 @@ Output: [0,1] The problem can be solved using a brute force approach, a hash table, or the two-pointer technique. The brute force approach has a time complexity of $$O(n^2)$$, while the hash table and two-pointer techniques have a time complexity of $$O(n)$$. The hash table approach is the most efficient and is recommended for large inputs. -### Approach 1: Brute Force (Naive) + + + ### Approach 1: Brute Force (Naive) The brute force approach is simple. We iterate through each element `nums[i]` and check if there is another element `nums[j]` such that `nums[i] + nums[j] == target`. If we find such a pair, we return the indices `[i, j]`. @@ -92,8 +94,12 @@ function twoSumProblem() { const result = twoSum(nums, target); return (
-

Input: nums = {"[", nums.join(", "), "]"}, target = {target}

-

Output: {"[", result.join(", "), "]"}

+

+ Input: nums = {("[", nums.join(", "), "]")}, target = {target} +

+

+ Output: {("[", result.join(", "), "]")} +

); } @@ -116,6 +122,7 @@ function twoSumProblem() { return []; } ``` +
```typescript @@ -131,6 +138,7 @@ function twoSumProblem() { return []; } ``` + ```python @@ -141,8 +149,9 @@ function twoSumProblem() { if nums[i] + nums[j] == target: return [i, j] - return [] + return [] ``` + ```java @@ -160,6 +169,7 @@ function twoSumProblem() { } } ``` + ```cpp @@ -178,10 +188,10 @@ function twoSumProblem() { } }; ``` +
- #### Complexity Analysis - Time Complexity: $$O(n^2)$$ @@ -191,6 +201,9 @@ function twoSumProblem() { - The space complexity is $$O(1)$$ because we are not using any extra space. - This approach is not efficient and is not recommended for large inputs. + + + ### Approach 2: Using Hash Table We can solve this problem more efficiently using a hash table. We iterate through the array and store the elements and their indices in a hash table. For each element `nums[i]`, we calculate the complement `target - nums[i]` and check if the complement is present in the hash table. If it is present, we return the indices `[i, numMap.get(complement)]`. If the complement is not present, we add the element `nums[i]` to the hash table. If no pair is found, we return an empty array. @@ -202,25 +215,29 @@ function twoSumProblem() { const nums = [2, 7, 11, 15]; const target = 9; - const twoSum = function (nums, target) { - const numMap = new Map(); + const twoSum = function (nums, target) { + const numMap = new Map(); - for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i]; - if (numMap.has(complement)) { - return [numMap.get(complement), i]; - } - numMap.set(nums[i], i); - } + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (numMap.has(complement)) { + return [numMap.get(complement), i]; + } + numMap.set(nums[i], i); + } - return []; - }; + return []; + }; const result = twoSum(nums, target); return (
-

Input: nums = {"[", nums.join(", "), "]"}, target = {target}

-

Output: {"[", result.join(", "), "]"}

+

+ Input: nums = {("[", nums.join(", "), "]")}, target = {target} +

+

+ Output: {("[", result.join(", "), "]")} +

); } @@ -245,6 +262,7 @@ function twoSumProblem() { return []; } ``` +
```typescript @@ -262,6 +280,7 @@ function twoSumProblem() { return []; } ``` + ```python @@ -276,6 +295,7 @@ function twoSumProblem() { return [] ``` + ```java @@ -295,6 +315,7 @@ function twoSumProblem() { } } ``` + ```cpp @@ -315,10 +336,10 @@ function twoSumProblem() { } }; ``` + - #### Complexity Analysis - Time Complexity: $$O(n)$$ @@ -331,6 +352,9 @@ function twoSumProblem() { - The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table. - The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$. + + + ### Approach 3: Using Two Pointers We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, `left` and `right`, to find the two numbers that add up to the target sum. We initialize `left` to `0` and `right` to `nums.length - 1`. We calculate the sum of the two numbers at the `left` and `right` pointers. If the sum is equal to the target, we return the indices `[left, right]`. If the sum is less than the target, we increment the `left` pointer. If the sum is greater than the target, we decrement the `right` pointer. If no pair is found, we return an empty array. @@ -366,8 +390,12 @@ function twoSumProblem() { const result = twoSum(nums, target); return (
-

Input: nums = {"[", nums.join(", "), "]"}, target = {target}

-

Output: {"[", result.join(", "), "]"}

+

+ Input: nums = {("[", nums.join(", "), "]")}, target = {target} +

+

+ Output: {("[", result.join(", "), "]")} +

); } @@ -399,6 +427,7 @@ function twoSumProblem() { return []; } ``` +
```typescript @@ -423,6 +452,7 @@ function twoSumProblem() { return []; } ``` + ```python @@ -443,6 +473,7 @@ function twoSumProblem() { return [] ``` + ```java @@ -473,6 +504,7 @@ function twoSumProblem() { } } ``` + ```cpp @@ -506,6 +538,7 @@ function twoSumProblem() { } }; ``` + @@ -519,6 +552,9 @@ function twoSumProblem() { - This approach is efficient and is recommended for large inputs. - The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$. + + + :::tip Note **Which is the best approach? and why?** @@ -528,4 +564,4 @@ The hash table approach is the most efficient and is recommended for large input ## Conclusion -In this tutorial, we learned how to solve the Two Sum problem on LeetCode using different approaches. We discussed the brute force approach, the hash table approach, and the two-pointer approach. We implemented the solutions in JavaScript, TypeScript, Python, Java, and C++. We also analyzed the time and space complexity of each approach and compared them to determine the best approach for this problem. The hash table approach is the most efficient and is recommended for large inputs. \ No newline at end of file +In this tutorial, we learned how to solve the Two Sum problem on LeetCode using different approaches. We discussed the brute force approach, the hash table approach, and the two-pointer approach. We implemented the solutions in JavaScript, TypeScript, Python, Java, and C++. We also analyzed the time and space complexity of each approach and compared them to determine the best approach for this problem. The hash table approach is the most efficient and is recommended for large inputs.