You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[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/)|
21
21
22
22
## Problem Description
@@ -34,7 +34,7 @@ You can return the answer in any order.
34
34
```plaintext
35
35
Input: nums = [2,7,11,15], target = 9
36
36
Output: [0,1]
37
-
```
37
+
```
38
38
39
39
**`Example 2:`**
40
40
@@ -67,7 +67,9 @@ Output: [0,1]
67
67
68
68
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.
69
69
70
-
### Approach 1: Brute Force (Naive)
70
+
<Tabs>
71
+
<tabItemvalue="Brute Force"label="Brute Force">
72
+
### Approach 1: Brute Force (Naive)
71
73
72
74
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]`.
- The space complexity is $$O(1)$$ because we are not using any extra space.
192
202
- This approach is not efficient and is not recommended for large inputs.
193
203
204
+
</tabItem>
205
+
<tabItem value="Hash Table" label="Hash Table">
206
+
194
207
### Approach 2: Using Hash Table
195
208
196
209
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.
- The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table.
332
353
- The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$.
333
354
355
+
</tabItem>
356
+
<tabItem value="Two Pointer" label="Two Pointer">
357
+
334
358
### Approach 3: Using Two Pointers
335
359
336
360
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.
- This approach is efficient and is recommended for large inputs.
520
553
- The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$.
521
554
555
+
</tabItem>
556
+
</Tabs>
557
+
522
558
:::tip Note
523
559
**Which is the best approach? and why?**
524
560
@@ -528,4 +564,4 @@ The hash table approach is the most efficient and is recommended for large input
528
564
529
565
## Conclusion
530
566
531
-
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 forthisproblem. The hash table approach is the most efficient and is recommended for large inputs.
567
+
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 forthisproblem. The hash table approach is the most efficient and is recommended for large inputs.
0 commit comments