Skip to content

Commit e34a434

Browse files
authored
Create 14. find all pairs in an array with a given sum.md
1 parent f6cbc23 commit e34a434

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
### Approach 1: Brute Force (Time Complexity: ( O(n^2) ))
2+
This method iterates over all possible pairs in the array and checks if the sum of the pair equals the target sum.
3+
```js
4+
function findPairsBruteForce(arr, targetSum) {
5+
const pairs = [];
6+
for (let i = 0; i < arr.length; i++) {
7+
for (let j = i + 1; j < arr.length; j++) {
8+
if (arr[i] + arr[j] === targetSum) {
9+
pairs.push([arr[i], arr[j]]);
10+
}
11+
}
12+
}
13+
return pairs;
14+
}
15+
16+
// Example usage
17+
const array = [1, 2, 3, 4, 5];
18+
const target = 6;
19+
console.log(findPairsBruteForce(array, target)); // Output: [[1, 5], [2, 4]]
20+
```
21+
### Approach 2: Using a Hash Table (Optimal, Time Complexity: ( O(n) ))
22+
This method uses a hash table to track numbers needed to reach the target sum. As you iterate through the array, you check if the current number exists in the hash table (indicating you've already encountered the complementary number).
23+
24+
```js
25+
function findPairsUsingHashTable(arr, targetSum) {
26+
const pairs = [];
27+
const seen = new Set(); // Use a Set to track visited numbers
28+
29+
for (const num of arr) {
30+
const complement = targetSum - num;
31+
32+
// Check if the complement exists in the set
33+
if (seen.has(complement)) {
34+
pairs.push([num, complement]);
35+
}
36+
37+
// Add the current number to the set for future checks
38+
seen.add(num);
39+
}
40+
41+
return pairs;
42+
}
43+
44+
// Example usage
45+
const array = [1, 2, 3, 4, 5];
46+
const target = 6;
47+
console.log(findPairsUsingHashTable(array, target)); // Output: [[4, 2], [5, 1]]
48+
```
49+
50+
### Approach 3: Two Pointers (Sorted Array, Time Complexity: ( O(n \log n) ) for sorting + ( O(n) ))
51+
This method works for sorted arrays. It uses two pointers — one starting at the beginning and one at the end of the array. Adjust the pointers based on the sum of the current pair.
52+
53+
```js
54+
function findPairsTwoPointers(arr, targetSum) {
55+
const pairs = [];
56+
arr.sort((a, b) => a - b); // Sort the array (required for two-pointer approach)
57+
58+
let left = 0;
59+
let right = arr.length - 1;
60+
61+
while (left < right) {
62+
const sum = arr[left] + arr[right];
63+
64+
if (sum === targetSum) {
65+
pairs.push([arr[left], arr[right]]);
66+
left++;
67+
right--;
68+
} else if (sum < targetSum) {
69+
left++; // Move the left pointer up
70+
} else {
71+
right--; // Move the right pointer down
72+
}
73+
}
74+
75+
return pairs;
76+
}
77+
78+
// Example usage
79+
const array = [1, 2, 3, 4, 5];
80+
const target = 6;
81+
console.log(findPairsTwoPointers(array, target)); // Output: [[1, 5], [2, 4]]
82+
```
83+
84+
| Approach | Time Complexity | Space Complexity | Notes |
85+
| :-- | :-- | :-- | :-- |
86+
| Brute Force | ( O(n^2) ) | ( O(1) ) | Inefficient for large arrays. Good for learning and testing. |
87+
| Using Hash Table | ( O(n) ) | ( O(n) ) | Fastest approach. Works well for unsorted arrays. |
88+
| Two Pointers (Sorted) | ( O(n \log n) ) | ( O(1) ) | Requires sorting first. Efficient for sorted arrays. |

0 commit comments

Comments
 (0)