Skip to content

Commit a709d5e

Browse files
committed
add article
1 parent d414458 commit a709d5e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

articles/two-integer-sum.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
## 1. Brute Force
22

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+
314
::tabs-start
415

516
```python
@@ -132,6 +143,20 @@ class Solution {
132143

133144
## 2. Sorting
134145

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 <a href="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+
135160
::tabs-start
136161

137162
```python
@@ -367,6 +392,20 @@ class Solution {
367392

368393
## 3. Hash Map (Two Pass)
369394

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+
370409
::tabs-start
371410

372411
```python
@@ -381,6 +420,7 @@ class Solution:
381420
diff = target - n
382421
if diff in indices and indices[diff] != i:
383422
return [i, indices[diff]]
423+
return []
384424
```
385425

386426
```java
@@ -544,6 +584,21 @@ class Solution {
544584

545585
## 4. Hash Map (One Pass)
546586

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.
600+
601+
547602
::tabs-start
548603

549604
```python

0 commit comments

Comments
 (0)