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: articles/two-integer-sum.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,16 @@
1
1
## 1. Brute Force
2
2
3
+
### Intuition
4
+
5
+
We can check every pair of different elements in the array and return the first pair that sums up to the target. This is the most intuitive approach but it's not the most efficient.
6
+
7
+
### Algorithm
8
+
9
+
1. Iterate through the array with two nested loops to check every pair of different elements.
10
+
2. If the sum of the pair equals the target, return the indices of the pair.
11
+
3. If no such pair is found, return an empty array.
12
+
4. There is guaranteed to be exactly one solution, so we will never return an empty array.
13
+
3
14
::tabs-start
4
15
5
16
```python
@@ -132,6 +143,20 @@ class Solution {
132
143
133
144
## 2. Sorting
134
145
146
+
### Intuition
147
+
148
+
We can sort the array and use two pointers to find the two numbers that sum up to the target. This is more efficient than the brute force approach. This approach is similar to the one used in <ahref="https://neetcode.io/problems/two-integer-sum-ii"target="_blank">Two Sum II</a>.
149
+
150
+
### Algorithm
151
+
152
+
1. Create a copy of the array and sort it in ascending order.
153
+
2. Initialize two pointers, one at the beginning and one at the end of the array.
154
+
3. Iterate through the array with the two pointers and check if the sum of the two numbers is equal to the target.
155
+
4. If the sum is equal to the target, return the indices of the two numbers.
156
+
5. If the sum is less than the target, move the left pointer to the right, which will increase the sum.
157
+
6. If the sum is greater than the target, move the right pointer to the left, which will decrease the sum.
158
+
7. There is guaranteed to be exactly one solution, so we will never return an empty array.
159
+
135
160
::tabs-start
136
161
137
162
```python
@@ -367,6 +392,20 @@ class Solution {
367
392
368
393
## 3. Hash Map (Two Pass)
369
394
395
+
### Intuition
396
+
397
+
We can use a hash map to store the value and index of each element in the array. Then, we can iterate through the array and check if the complement of the current element exists in the hash map. The complement must be at a different index, because we can't use the same element twice.
398
+
399
+
By using a hashmap, we can achieve a time complexity of $O(n)$ because the insertion and lookup time of a hashmap is $O(1)$.
400
+
401
+
### Algorithm
402
+
403
+
1. Create a hash map to store the value and index of each element in the array.
404
+
2. Iterate through the array and compute the complement of the current element, which is `target - nums[i]`.
405
+
3. Check if the complement exists in the hash map.
406
+
4. If it does, return the indices of the current element and its complement.
407
+
5. If no such pair is found, return an empty array.
408
+
370
409
::tabs-start
371
410
372
411
```python
@@ -381,6 +420,7 @@ class Solution:
381
420
diff = target - n
382
421
if diff in indices and indices[diff] != i:
383
422
return [i, indices[diff]]
423
+
return []
384
424
```
385
425
386
426
```java
@@ -544,6 +584,21 @@ class Solution {
544
584
545
585
## 4. Hash Map (One Pass)
546
586
587
+
### Intuition
588
+
589
+
We can solve the problem in a single pass by iterating through the array and checking if the complement of the current element exists in the hash map.
590
+
591
+
If it does, we return the indices of the current element and its complement. If not, we store the current element in the hash map. This guarantees that we will never use the same element twice, but we still check every element in the array.
592
+
593
+
### Algorithm
594
+
595
+
1. Create a hash map to store the value and index of each element in the array.
596
+
2. Iterate through the array and compute the complement of the current element, which is `target - nums[i]`.
597
+
3. Check if the complement exists in the hash map.
598
+
4. If it does, return the indices of the current element and its complement.
599
+
5. If no such pair is found, return an empty array.
0 commit comments