15 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solution set must not contain duplicate triplets.
Attempt 1: Brute force, O(N^3 + 2NlogN), O(N) space, TLE
Attempt 2: TLE
The idea is that if we sort our array, we'll be able to inch our way closer to the solution.
We maintain three 'pointers', main, iter, and max - and control where these point to depending on the current sum.
If the current sum is negative, then we check the next combination that will make the next current sum less negative. We do this by incrementing the iterator.
If the current sum is greater than zero, then we have overestimated. Decrementing the max index will ensure that the next current sum will be less positive.
When we find a result, then the iterator should increment to find the next unique solution. However, because we know that doing so will make the next current sum positive, we counter act that by decrementing max.
A set is used to maintain uniqueness.
Attempt 3: O(n^2), TLE
Approach:
I applied that same idea in 2Sum, which runs O(n).
In 2sum, the idea what to iterate through the array and find a valid target, which if
a + b = target
, then we hash to try and finda = target - b
.In 3sum, I took the same approach. If
a + b + c = target
, then I hash to finda + b = target - c
.Now I'll have to iterate through permutational pairs of a and b, which will take O(n^2) with a double for loop.
I ensure that the second for loop never overlaps with the first and avoids duplicates by having it set to
j = i + 1
.In over to find all pair triplets that sum to zero and avoid duplicates at the same time, I used to two checks:
The first, is
is_valid_number
, which check for a few things. One, the numbertarget - c
or-(a + b)
must exist in the hash table. And secondly, if it does and there are duplicates, e.g.-1,-1,2
, wherea=b
, then it must also mean that the occurrences for -1 must be > 2. Likewise if-1,2,-1
, thentarget = a
, so the occurrences must be greater than 1.
Secondly, I maintain a Permutation
unordered_set
that captures only unique triples in O(1) time. The idea for this is explained in my other gitbook, chapterHash by Custom Object
.
Attempt 4: O(n^2), TLE
I was pretty confident that I got this round, but all the extra processing that goes into converting for a set of tuples to a list, adds come constant time to the complexity.
The idea goes as follows. In the first approach, we iterated through pairs of i
, j
and k
, where i=0
, j=i+1
, and k=j+1
. The idea here is the same, except that we iterate though unique combinations of i
, j
, and use 2 sum to find valid k
elements and will make numbers of i
and j
sum to 0. To do this, we hash every number in the array as a key to a list of unique indices. This way, when we find more than one possible target in the array, we can iterate though all valid solutions and append them to our array. The key here is that just like i
, j
, and k
were all unique in the brute force solution, we can check for all i
, j
, k
indices here as a set to have a length of 3 in order to indicate that they are unique as well. Additionally, if we do find a valid set, in order to avoid provided solutions that already exist, we store the sorted tuple into a set. This way, the set datastructure can identify unique sets.
Last updated