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
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md
+40-37Lines changed: 40 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -13,15 +13,13 @@ tags:
13
13
description: "This is a solution to the Two Sum problem on LeetCode."
14
14
---
15
15
16
-
We have provided multiple solutions to the Two Sum problem on LeetCode. The problem is to find two numbers in an array that add up to a target sum. We have provided solutions using a brute force approach, a hash table, and the two-pointer technique. The hash table approach is the most efficient among the three approaches and is recommended for large inputs.
16
+
## Problem Description
17
17
18
18
|Problem Statement | Solution Link | LeetCode Profile|
19
19
|:---|:---|:---|
20
20
|[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
-
LeetCode provides the [Two Sum problem](https://leetcode.com/problems/two-sum/) with the following problem statement:
23
-
24
-
### Problem Description
22
+
## Problem Description
25
23
26
24
Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.
27
25
@@ -31,18 +29,23 @@ You can return the answer in any order.
31
29
32
30
### Examples
33
31
34
-
```text
32
+
**`Example 1:`**
33
+
34
+
```plaintext
35
35
Input: nums = [2,7,11,15], target = 9
36
36
Output: [0,1]
37
-
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
38
-
```
37
+
```
39
38
40
-
```text
39
+
**`Example 2:`**
40
+
41
+
```plaintext
41
42
Input: nums = [3,2,4], target = 6
42
43
Output: [1,2]
43
44
```
44
45
45
-
```text
46
+
**`Example 3:`**
47
+
48
+
```plaintext
46
49
Input: nums = [3,3], target = 6
47
50
Output: [0,1]
48
51
```
@@ -54,11 +57,15 @@ Output: [0,1]
54
57
-`-10^9 <= target <= 10^9`
55
58
- Only one valid answer exists.
56
59
60
+
**Follow up:** Can you come up with an algorithm that is less than <code>$$O(n^2)$$</code> time complexity?
61
+
62
+
---
63
+
57
64
## Solution for Two Sum Problem
58
65
59
-
### Intuition
66
+
### Intuition and Approach
60
67
61
-
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<sup>2</sup>)_**, the hash table approach has a time complexity of **_O(n)_**, and the two-pointer technique has a time complexity of **_O(n log n)_**.
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.
62
69
63
70
### Approach 1: Brute Force (Naive)
64
71
@@ -177,16 +184,16 @@ function twoSumProblem() {
177
184
178
185
#### Complexity Analysis
179
186
180
-
- Time Complexity:**_O(n<sup>2</sup>)_**
181
-
- Space Complexity:**_O(1)_**
187
+
- Time Complexity:$$O(n^2)$$
188
+
- Space Complexity:$$O(1)$$
182
189
- Where `n` is the length of the input array `nums`.
183
-
- The time complexity is **_O(n<sup>2</sup>)_** because we are iterating through the array twice.
184
-
- The space complexity is **_O(1)_** because we are not using any extra space.
190
+
- The time complexity is $$O(n^2)$$ because we are iterating through the array twice.
191
+
- The space complexity is $$O(1)$$ because we are not using any extra space.
185
192
- This approach is not efficient and is not recommended for large inputs.
186
193
187
194
### Approach 2: Using Hash Table
188
195
189
-
We can improve the time complexity of the brute force approach by using a hash table to store the elements and their indices. While we iterate through the array, we check ifthe difference between the target and the current element existsin the hash table. If it does, we return the indices ofthe current element and the element that makes up the target sum.
196
+
We can solve this problem more efficiently using a hash table. We iterate through the array and store the elements and their indicesin a hash table. For each element `nums[i]`, we calculate the complement `target - nums[i]` and check ifthe complement is presentin the hash table. If it is present, we return the indices `[i, numMap.get(complement)]`. Ifthe complement is not present, we add the element `nums[i]` to the hash table. If no pair is found, we return an empty array.
190
197
191
198
#### Implementation
192
199
@@ -314,19 +321,19 @@ function twoSumProblem() {
314
321
315
322
#### Complexity Analysis
316
323
317
-
- Time Complexity:**_O(n)_**
318
-
- Space Complexity:**_O(n)_**
324
+
- Time Complexity:$$O(n)$$
325
+
- Space Complexity:$$O(n)$$
319
326
- Where `n` is the length of the input array `nums`.
320
-
- The time complexity is **_O(n)_** because we iterate through the array only once.
321
-
- The space complexity is **_O(n)_** because we use a hash table to store the elements and their indices.
327
+
- The time complexity is $$O(n)$$ because we iterate through the array only once.
328
+
- The space complexity is $$O(n)$$ because we use a hash table to store the elements and their indices.
322
329
- This approach is more efficient than the brute force approach and is recommended for large inputs.
323
-
- The hash table lookup has an average time complexity of**_O(1)_**.
324
-
- The space complexity is **_O(n)_** because we store at most `n` elements in the hash table.
325
-
- The total time complexity is **_O(n)_**. and the total space complexity is **_O(n)_**.
330
+
- The hash table lookup has an average time complexity of$$O(1)$$, which makes this approach efficient.
331
+
- The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table.
332
+
- The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$.
326
333
327
334
### Approach 3: Using Two Pointers
328
335
329
-
We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, one at the beginning and one at the end. Wemove the pointers based on the sum of the elements at the two pointers. If the sum is equal to the target, we return the indices of the two elements. If the sum is less than the target, we move the left pointer to the right. If the sum is greater than the target, we move the right pointer to the left.
336
+
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. Weinitialize `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.
330
337
331
338
#### Implementation
332
339
@@ -504,25 +511,21 @@ function twoSumProblem() {
504
511
505
512
#### Complexity Analysis
506
513
507
-
- Time Complexity:**_O(n log n)_**
508
-
- Space Complexity:**_O(n)_**
514
+
- Time Complexity:$$O(n \log n)$$
515
+
- Space Complexity:$$O(n)$$
509
516
- Where `n` is the length of the input array `nums`.
510
-
- The time complexity is **_O(n log n)_** because we sort the array.
511
-
- The space complexity is **_O(n)_** because we store the indices of the elements in the sorted array.
517
+
- The time complexity is $$O(n \log n)$$ because we sort the array.
518
+
- The space complexity is $$O(n)$$ because we store the indices of the elements in the sorted array.
512
519
- This approach is efficient and is recommended for large inputs.
513
-
- The total time complexity is **_O(n log n)_**. and the total space complexity is **_O(n)_**.
520
+
- The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$.
514
521
515
522
:::tip Note
516
523
**Which is the best approach? and why?**
517
524
518
-
The hash table approach is the most efficient among the three approaches. It has a time complexity of**_O(n)_** and is recommended for large inputs. The two-pointer approach also has a time complexity of**_O(n log n)_** but requires sorting the array, which may not be necessary in some cases. The brute force approach is the least efficient with a time complexity of**_O(n<sup>2</sup>)_** and is not recommended for large inputs.
519
-
:::
525
+
The hash table approach is the most efficient and is recommended for large inputs. The hash table lookup has an average time complexity of$$O(1)$$, which makes this approach efficient. The time complexity of the hash table approach is $$O(n)$$, which is better than the brute force approach with a time complexity of$$O(n^2)$$ and the two-pointer approach with a time complexity of$$O(n \log n)$$. The space complexity of the hash table approach is $$O(n)$$, which is the same as the two-pointer approach. Therefore, the hash table approach is the best approach forthis problem.
520
526
521
-
## Summary
527
+
:::
522
528
523
-
In this problem, we learned how to find two numbers in an array that add up to a target sum. We explored three different approaches to solve this problem:
529
+
## Conclusion
524
530
525
-
1. The brute force approach with a time complexity of**_O(n<sup>2</sup>)_**.
526
-
2. The hash table approach with a time complexity of**_O(n)_**.
527
-
3. The two-pointer approach with a time complexity of**_O(n log n)_**.
528
-
4. The hash table approach is the most efficient and is recommended for large inputs.
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.
0 commit comments