# Find All Triplets With Zero Sum

Posted: 10 Nov, 2020

Difficulty: Moderate

#### You are given an array Arr consisting of n integers, you need to find all the distinct triplets present in the array which adds up to zero.

#### An array is said to have a triplet {arr[i], arr[j], arr[k]} with 0 sum if there exists three indices i, j and k such that i!=j, j!=k and i!=k and arr[i] + arr[j] + arr[k] = 0.

##### Note :

```
1. You can return the list of values in any order. For example, if a valid triplet is {1, 2, -3}, then (2, -3, 1), (-3, 2, 1) etc is also valid triplet. Also, the ordering of different triplets can be random i.e if there are more than one valid triplets, you can return them in any order.
2. The elements in the array need not be distinct.
3. If no such triplet is present in the array, then return an empty list, and the output printed for such a test case will be "-1".
```

##### Input Format :

```
The first line of the input contains an integer T, denoting the number of test cases.
The first line of each test case contains the integer N, denoting the size of the array.
The second line of each test case contains N space-separated integers denoting the array elements.
```

##### Output Format :

```
For each test case, every line of output contains three spaced integers that correspond to the elements which add to zero. Refer to sample input 2 for more clarification.
```

##### Note :

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

##### Constraints :

```
1 <= T <= 50
1 <= N <= 10^3
-10^5 <= Arr[i] <= 10^5
Time Limit: 1 sec
```

Approach 1

- The most trivial approach would be to find all triplets of the array and count all such triplets whose sum = 0.
- We can find the answer using three nested loops for three different indexes and check if the sum of values at those indexes adds up to zero.
- Create a set to keep the track of triplets we have visited. Run first loop from i = 0 to i = n - 3, second loop from j = i + 1 to j = n - 2 and third loop from k = j + 1 to k = n - 1.
- Check if arr[i] + arr[j] + arr[k] = 0
- If the triplet is not present in the set, then print the triplet and insert triplet into the set since we need distinct triplets only.
- Else continue.

Approach 2

- Sort the array in non-decreasing order because after the array is sorted, we don’t have to process the same elements multiple times and hence we don’t have to explicitly keep track for distinct triplets.
- Now since we want triplets such that x + y + z = 0, we have x+ y = -z and now we can fix z as arr[i]. So we want to find sum of two numbers x and y as -arr[i] in the array.
- Let us assume that we are the ith index of the array and initialise variable target to -arr[i].
- So now we just need to find two elements x, y such that target = x + y.
- We will use two pointers, one will start from i+1, and other will start from the end of the array.
- Let the two pointers be front and back, where front = i + 1 and back = n - 1. Let sum = x + y, where x = arr[front] and y = arr[back]. We have to find the triplets such that target = sum.
- While front < back, there will be 3 cases:
- if(sum < target), we will have to increase the sum and hence increment front pointer.
- Else if(sum > target), we will have to decrease the sum and hence decrease back pointer.
- Else print the triplet and since we want distinct triplets, do the following.
- Increment the front pointer until arr[front] = x and front < back.
- Decrement the back pointer until arr[back] = y and front < back.

- While arr[i] = arr[i+1], keep on incrementing i.