|  | 
|  | 1 | +# 0011 ThreeSum ( L-I ) | 
|  | 2 | + | 
|  | 3 | +## Problem | 
|  | 4 | +Three Sum (3SUM) | 
|  | 5 | + | 
|  | 6 | +Given an array nums of n integers, are there elements  `a, b, c`   in nums such that   `a + b + c = target` ? Find all unique triplets in the array which gives the sum is equal to  `target`  . | 
|  | 7 | + | 
|  | 8 | + | 
|  | 9 | + | 
|  | 10 | +## Solution | 
|  | 11 | + | 
|  | 12 | +```javascript | 
|  | 13 | +// 1. Brute Force Solution | 
|  | 14 | +const threeSum_brute_Force = (nums, target) => { | 
|  | 15 | + let result = []; | 
|  | 16 | + for (let i = 0; i < nums.length; i++) { | 
|  | 17 | + for (let j = i + 1; j < nums.length; j++) { | 
|  | 18 | + for (let k = j + 1; k < nums.length; k++) { | 
|  | 19 | + if (nums[i] + nums[j] + nums[k] === target) { | 
|  | 20 | + result.push([nums[i], nums[j], nums[k]]); | 
|  | 21 | + } | 
|  | 22 | + } | 
|  | 23 | + } | 
|  | 24 | + } | 
|  | 25 | + return result; | 
|  | 26 | +} | 
|  | 27 | + | 
|  | 28 | +// 2. Using a hash table | 
|  | 29 | +const threeSum_hash_table = (nums, target) => { | 
|  | 30 | + let result = []; | 
|  | 31 | + const hash = {}; | 
|  | 32 | + for (let i = 0; i < nums.length; i++) { | 
|  | 33 | + for (let j = i + 1; j < nums.length; j++) { | 
|  | 34 | + const complement = target - (nums[i] + nums[j]); | 
|  | 35 | + if (complement in hash) { | 
|  | 36 | + result.push([nums[i], nums[j], complement]); | 
|  | 37 | + } else { | 
|  | 38 | + hash[nums[j]] = true; | 
|  | 39 | + } | 
|  | 40 | + } | 
|  | 41 | + } | 
|  | 42 | + | 
|  | 43 | + return result; | 
|  | 44 | +} | 
|  | 45 | + | 
|  | 46 | +// 3. Two Pointer Technique | 
|  | 47 | +const threeSum_two_pointer = (nums, target) => { | 
|  | 48 | + let result = []; | 
|  | 49 | + nums.sort((a, b) => a - b); | 
|  | 50 | + for (let i = 0; i < nums.length - 2; i++) { | 
|  | 51 | + if (i > 0 && nums[i] === nums[i - 1]) continue; | 
|  | 52 | + let left = i + 1; | 
|  | 53 | + let right = nums.length - 1; | 
|  | 54 | + while (left < right) { | 
|  | 55 | + const sum = nums[i] + nums[left] + nums[right]; | 
|  | 56 | + if (sum === target) { | 
|  | 57 | + result.push([nums[i], nums[left], nums[right]]); | 
|  | 58 | + while (left < right && nums[left] === nums[left + 1]) left++; | 
|  | 59 | + while (left < right && nums[right] === nums[right - 1]) right--; | 
|  | 60 | + left++; | 
|  | 61 | + right--; | 
|  | 62 | + } else if (sum < target) { | 
|  | 63 | + left++; | 
|  | 64 | + } else { | 
|  | 65 | + right--; | 
|  | 66 | + } | 
|  | 67 | + } | 
|  | 68 | + } | 
|  | 69 | + return result; | 
|  | 70 | +} | 
|  | 71 | +``` | 
|  | 72 | + | 
|  | 73 | +## How it works | 
|  | 74 | +### 1. Brute Force Steps | 
|  | 75 | + | 
|  | 76 | +1. Initialize an empty array result to store the unique triplets that sum to the target value. | 
|  | 77 | + | 
|  | 78 | +2. Use three nested loops to iterate through the elements of the nums array. The outermost loop (indexed by i) iterates from the first element to the second-to-last element. | 
|  | 79 | + | 
|  | 80 | +3. The second loop (indexed by j) iterates from the element after the i-th element to the second-to-last element. | 
|  | 81 | + | 
|  | 82 | +4. The innermost loop (indexed by k) iterates from the element after the j-th element to the last element of the nums array. | 
|  | 83 | + | 
|  | 84 | +5. Within the innermost loop, check if the sum of the elements at indices i, j, and k is equal to the target value. | 
|  | 85 | + | 
|  | 86 | +6. If the sum is equal to the target, create a triplet by selecting the elements at these indices (nums[i], nums[j], nums[k]) and push it into the result array. | 
|  | 87 | + | 
|  | 88 | +7. Continue iterating through the loops to find all such triplets. | 
|  | 89 | + | 
|  | 90 | +8. Finally, return the result array containing all unique triplets that sum to the target value. | 
|  | 91 | + | 
|  | 92 | +### 2. Hash Table Steps | 
|  | 93 | + | 
|  | 94 | +1. Initialize an empty array result to store the unique triplets. | 
|  | 95 | +2. Create an empty hash table hash to store the values encountered in the inner loop. | 
|  | 96 | +3. Use two nested loops to iterate through the nums array. | 
|  | 97 | +4. Calculate the complement by subtracting the sum of nums[i] and nums[j] from the target. | 
|  | 98 | +5. Check if the complement exists in the hash table. | 
|  | 99 | +6. If found, add [nums[i], nums[j], complement] to the result array. | 
|  | 100 | +7. If not found, store nums[j] in the hash table. | 
|  | 101 | +8. Return the result array containing all unique triplets that sum to the target value. | 
|  | 102 | + | 
|  | 103 | +### 3. Two Pointer Technique Steps | 
|  | 104 | + | 
|  | 105 | +1. Sort the nums array in ascending order. | 
|  | 106 | +2. Iterate through the sorted array with a loop, selecting an element nums[i] as the first element of a potential triplet. | 
|  | 107 | +3. Use two pointers, left and right, to search for a pair of elements that sum up to the complement of nums[i] (i.e., target - nums[i]). | 
|  | 108 | +4. If found, add [nums[i], nums[left], nums[right]] to the result array, and move both pointers while skipping duplicates. | 
|  | 109 | +5. Continue the iteration until all unique triplets that sum to the target are found. | 
|  | 110 | +6. Return the result array containing all unique triplets. | 
|  | 111 | + | 
|  | 112 | + | 
|  | 113 | +## References | 
|  | 114 | +- [Wikipedia](https://en.wikipedia.org/wiki/3SUM) | 
|  | 115 | +- [Leetcode](https://leetcode.com/problems/3sum/) | 
|  | 116 | + | 
|  | 117 | + | 
|  | 118 | +## Problem Added By | 
|  | 119 | +- [PortFolio](https://femil-savaliya.netlify.app) | 
|  | 120 | +- [GitHub](https://www.github.com/Femil32)  | 
|  | 121 | +- [LinkedIn](https://www.linkedin.com/in/femil-savaliya) | 
|  | 122 | + | 
|  | 123 | + | 
|  | 124 | +## Contributing | 
|  | 125 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | 
|  | 126 | + | 
|  | 127 | +Please make sure to update tests as appropriate. | 
0 commit comments