Given an integer array
Notice that the solution set must not contain duplicate triplets.
Example 1
Output: [[-1, 0, 1], [-1, -1, 2]]
Explanation: There are three triplets which gives sum as 0.
nums[0] + nums[1] + nums[2] which is -1 + 0 + 1 = 0
nums[1] + nums[2] + nums[4] which is 0 + 1 + -1 = 0
nums[0] + nums[3] + nums[4] which is -1 + 2 + -1 = 0
Notice that first and second triplets are same, so the unique triplets are [[-1, 0, 1], [-1, -1, 2]].
Example 2
Output: []
Explanation: There are no triplets which sum up to 0.
Example 3
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.
Constraints:
-
3 <= nums.length <= 3000 -
-105 <= nums[i] <= 105
Contents
- In this approach, we will split the problem into a sub problem of 2 Sum using below steps.
-
As per the problem statement, we have to find a triplet from the array which sum up to zero,
array[i] + array[j] + array[k] =0 .
We can rewrite this asarray[j] + array[k] = 0 - array[i] . - So, as we loop the array, for each number at
i th index from the array, we will need to find out a pair of numbers at indexj andk that adds up to 0 - array[i].
- We will use an intermediate data structure HashSet to track of duplicates.
Complexity Analysis:
Above code runs in O(n2) time complexity where
- In this approach, we will first sort the input array, so that identifying duplicate triplets is easy and then make the problem into a sub problem of 2 Sum II using the same steps in this solution.
-
Once the input array is sorted, loop through elements from 0th index to array.length - 2th index
and for each element, check if ith element is same as i-1th element and ignore if they are same.
Reason is that, if a triplet is found at i-1th index, it will get same triplets at ith index as well if values arei andi-1 indexes are same. -
Next, compute remaining sum needed to make it 0,
twoSum = 0-array[i] and initialize two pointer variableslow = i +1 andhigh = array.length-1 to iterate from left and right ends of array respectively until both pointers meet each other. -
If you find two numbers such that
array[low] + array[high] == twoSum , then we have found one triplet. Now continue checking other possible pairs that adds upto the sametwoSum , we also need to avoid duplicates while doing this.
-
Increment
low to next indexlow = low +1 , and if that is within the bounds of input array, check ifnums[i] == nums[i-1] , if they are same then keep incrementinglow . -
We don't need to check right side values, as it automatically gets eliminated, say for example
twoSum = 4 and nums[2, 2, 3, 2, 2, 2] andlow = 0 andhigh =5 when the two sum match is found. If we eliminate left side duplicates, we will updatelow = 2 to point nums[2]= 3, right side values automatically gets eliminated as sum will become either higher or lower than target.
-
If you find two numbers such that
array[low] + array[high] < twoSum , then movelow pointer by 1. -
If you find two numbers such that
array[low] + array[high] > twoSum , then decrementhigh pointer by 1.
Complexity Analysis:
Above code runs in O(n2) time complexity where
Above examples source code can be found at GitHub link for Java code and JUnit tests can be found at GitHub link for Unit tests code