## Three Number Sum

Write a function that takes in a non-empty array of distinct integers and an integer representing a target sum. The function should find all triplets in the array that sum up to the target sum and return a two-dimensional array of all these triplets. The numbers in each triplet should be ordered in ascending order, and the triplets themselves should be ordered in ascending order with respect to the numbers they hold.

If no three numbers sum up to the target sum, the function should return an empty array.

### Sample Input

`1array = [12, 3, 1, 2, -6, 5, -8, 6]2targetSum = 0`

### Sample Output

`1[[-8, 2, 6], [-8, 3, 5], [-6, 1, 5]]`

### Hints

**Hint 1**

Using three for loops to calculate the sums of all possible triplets in the array would generate an algorithm that runs in O(n^3) time, where n is the length of the input array. Can you come up with something faster using only two for loops?̄̄̄

**Hint 2**

Try sorting the array and traversing it once. At each number, place a left pointer on the number immediately to the right of your current number and a right pointer on the final number in the array. Check if the current number, the left number, and the right number sum up to the target sum. How can you proceed from there, remembering the fact that you sorted the array?

**Hint 3**

Since the array is now sorted (see Hint #2), you know that moving the left pointer mentioned in Hint #2 one place to the right will lead to a greater left number and thus a greater sum. Similarly, you know that moving the right pointer one place to the left will lead to a smaller right number and thus a smaller sum. This means that, depending on the size of each triplet’s (current number, left number, right number) sum relative to the target sum, you should either move the left pointer, the right pointer, or both to obtain a potentially valid triplet.

**Optimal Space & Time Complexity**

O(n^2) time | O(n) space - where n is the length of the input array

```
Solution-11function threeNumberSum(array, targetSum) {2
3 /*4 * Brute Force approach5 * Sort the given array6 * Write three nested loops7 * Do the sum in every nested loop8 * if equal to target sum, add it to the returning list9 * return at last10 */11
12 array.sort((a,b) => a - b)13
14 let returningList = []15 16 for (let i = 0; i < array.length; i++) {17 for (let j = i+1; j < array.length; j++) {18 for (let k = j+1; k < array.length; k++) {19 if (array[i] + array[j] + array[k] === targetSum) {20 returningList.push([array[i], array[j], array[k]])21 }22 }23 }24 }25
26 return returningList;27}
```

`Solution-21function threeNumberSum(array, targetSum) {2 // Write your code here.3 array.sort((a, b) => a - b)4 5 let returningArray = []6 for (let i = 0; i < array.length; i++) {7 left = i + 1;8 right = array.length - 1;9 while (left < right) {10 let sum = array[i] + array[left] + array[right];11 if (sum === targetSum) {12 returningArray.push([array[i], array[left], array[right]]);13 left++;14 right--;15 } else if (sum < targetSum) {16 left++;17 } else if (sum > targetSum) {18 right--;19 }20 }21 }22 return returningArray;23}`

🎮