Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 27d7bd0

Browse files
authored
Create 15. Find all triplets in an array with a given sum.md
1 parent e34a434 commit 27d7bd0

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
### Using Sorting and Two Pointers (Time Complexity (O(n^2)))
2+
This approach is more efficient and first sorts the array. For every number in the array, it reduces the problem to finding pairs (similar to the two-pointer method).
3+
4+
Algorithm:
5+
Sort the input array.
6+
Fix one number (arr[i) and use two pointers (left and right) to find two other numbers that sum up to targetSum - arr[i].
7+
8+
```js
9+
function findTripletsTwoPointers(arr, targetSum) {
10+
const triplets = [];
11+
arr.sort((a, b) => a - b); // Sort the array
12+
13+
for (let i = 0; i < arr.length - 2; i++) {
14+
// Skip duplicate values to prevent repetitive triplets
15+
if (i > 0 && arr[i] === arr[i - 1]) continue;
16+
17+
let left = i + 1;
18+
let right = arr.length - 1;
19+
20+
while (left < right) {
21+
const sum = arr[i] + arr[left] + arr[right];
22+
23+
if (sum === targetSum) {
24+
triplets.push([arr[i], arr[left], arr[right]]);
25+
26+
// Skip duplicate values to prevent repetitive triplets
27+
while (left < right && arr[left] === arr[left + 1]) left++;
28+
while (left < right && arr[right] === arr[right - 1]) right--;
29+
30+
left++;
31+
right--;
32+
} else if (sum < targetSum) {
33+
left++; // Increase the sum
34+
} else {
35+
right--; // Decrease the sum
36+
}
37+
}
38+
}
39+
40+
return triplets;
41+
}
42+
43+
// Example usage
44+
const array = [1, 2, 3, 4, 5, 6, 1];
45+
const target = 10;
46+
console.log(findTripletsTwoPointers(array, target)); // Output: [[1, 3, 6], [1, 4, 5], [2, 3, 5], [1, 3, 6]]
47+
```
48+
49+
### Using Hashing (Time Complexity (O(n^2)), Space Complexity (O(n)))
50+
This approach uses a hash table to track numbers for each fixed element. It avoids sorting and directly checks for valid pairs that sum to targetSum - arr[i].
51+
52+
Algorithm:
53+
Fix one number and use a hash table to find the remaining two numbers.
54+
For each pair of numbers, check if a third number exists in the hash table to complete the triplet.
55+
56+
```js
57+
function findTripletsUsingHashing(arr, targetSum) {
58+
const triplets = [];
59+
const n = arr.length;
60+
61+
for (let i = 0; i < n - 2; i++) {
62+
const seen = new Set(); // Hash table to store complements
63+
const currentTarget = targetSum - arr[i];
64+
65+
for (let j = i + 1; j < n; j++) {
66+
const complement = currentTarget - arr[j];
67+
if (seen.has(complement)) {
68+
triplets.push([arr[i], arr[j], complement]);
69+
}
70+
seen.add(arr[j]); // Add the current number to the hash table
71+
}
72+
}
73+
74+
return triplets;
75+
}
76+
77+
// Example usage
78+
const array = [1, 2, 3, 4, 5, 6];
79+
const target = 10;
80+
console.log(findTripletsUsingHashing(array, target)); // Output: [[1, 4, 5], [2, 3, 5]]
81+
```
82+
83+
| Approach | Time Complexity | Space Complexity | Notes |
84+
| :-- | :-- | :-- | :-- |
85+
| Brute Force | (O(n^3)) | (O(1)) | Very inefficient for large arrays. Avoid using this in real-world apps. |
86+
| Two Pointers (Sorting) | (O(n^2)) | (O(1)) | Efficient and handles duplicate values. Requires sorting the array. |
87+
| Hashing | (O(n^2)) | (O(n)) | Efficient but uses extra space for hash table. No need to sort array. |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /