# GeetCode Hub

Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.

Notice that the solution set must not contain duplicate triplets.

Example 1:

```Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
```

Example 2:

```Input: nums = []
Output: []
```

Example 3:

```Input: nums = 
Output: []
```

Constraints:

• `0 <= nums.length <= 3000`
• `-105 <= nums[i] <= 105`

class Solution { public List<List<Integer>> threeSum(int[] nums) { int len = nums.length; List<List<Integer>> res = new ArrayList<List<Integer>>(); if(len == 0){ return res; } Arrays.sort(nums); for(int i=0;i<len-2;i++){ if(i==0 || nums[i] != nums[i-1]){ int start = i+1; int end = len-1; int requiredSum = -nums[i]; while(start < end){ int curr = nums[start] + nums[end]; if(curr == requiredSum){ List<Integer> ans = new ArrayList<Integer>(); ans.add(nums[i]); ans.add(nums[start]); ans.add(nums[end]); res.add(ans); while(start < end && nums[start] == nums[start+1]){ start++; } while(start < end && nums[end] == nums[end-1]){ end--; } start++; end--; } else if(curr > requiredSum){ end--; } else{ start++; } } } } return res; } }

The main gist of this question is to reduce the 3 sum problem into a 2 sum problem and using the two pointer approach to solve it.

Time Complexity: O(n^2)
Space Complexity: O(n)  // for storing the result