Last Updated: April 2, 2026
We need to find all unique triplets (three elements) in the array that add up to zero. Two key things make this harder than Two Sum.
First, we need all valid triplets, not just one. Second, we need to avoid duplicate triplets in our result. For example, if the array has two -1s, both [-1, 0, 1] using the first -1 and [-1, 0, 1] using the second -1 count as the same triplet. We only want it once.
The core insight is this: if we fix one element nums[i], we need the other two elements to sum to -nums[i]. That's exactly the Two Sum problem. So 3Sum reduces to running Two Sum for each element in the array.
3 <= nums.length <= 3000 → With n up to 3000, O(n^2) gives us about 9 million operations, which is fine. O(n^3) gives 27 billion, which is too slow.-10^5 <= nums[i] <= 10^5 → Values fit in a standard integer. No overflow concerns for sums of three elements.The most straightforward approach is to check every possible combination of three elements. For each triplet (i, j, k), check if nums[i] + nums[j] + nums[k] == 0. If it does, add it to the result.
The tricky part is handling duplicates. We can sort each found triplet and store it in a set to ensure uniqueness. This way, [-1, 0, 1] and [0, -1, 1] both normalize to [-1, 0, 1] and the set catches the duplicate.
(i, j, k) where i < j < k.nums[i] + nums[j] + nums[k] == 0.The brute force checks every possible combination, which is far too slow for n up to 3000. If we fix one element and reduce the problem to Two Sum, can we find pairs more efficiently using a hash set?
Here's the key insight: if we fix one element nums[i], we need two other elements that sum to -nums[i]. That's literally the Two Sum problem. And we know Two Sum can be solved in O(n) using a hash set.
So the plan is: for each element nums[i], run a Two Sum search on the remaining elements looking for pairs that sum to -nums[i]. We use a hash set to find complements in O(1).
For duplicate avoidance, we first sort the array. Then we skip over duplicate values for the outer loop. For the inner Two Sum, we also need to be careful about adding duplicate triplets, which we handle by checking against the last triplet we added.
i from 0 to n-3:nums[i] is the same as nums[i-1] (avoid duplicate first elements).target = -nums[i].nums[i+1...n-1] that sum to target.nums[j], compute complement = target - nums[j].j.nums[j] to the set.This approach is already O(n^2), which is optimal for this problem. But we're using O(n) extra space for the hash set in each iteration. Can we achieve the same time complexity without the hash set by using two pointers on the sorted array?
Since we need to sort the array anyway (for duplicate handling), we can take full advantage of the sorted order. After fixing nums[i], we need two elements in the remaining sorted subarray that sum to -nums[i]. In a sorted array, finding a pair with a given sum is a classic two-pointer problem.
Place one pointer left at the start of the remaining subarray (i+1) and another pointer right at the end. If the sum of the three elements is too small, move left right to increase it. If it's too large, move right left to decrease it. If it's exactly zero, we found a triplet.
The beauty of this approach is that each pointer moves in only one direction, so the inner search is O(n). Combined with the outer loop, we get O(n^2) total with O(1) extra space.
The two-pointer technique works because the array is sorted. When the sum is too small, moving the left pointer right increases it (since nums[left+1] >= nums[left]). When the sum is too large, moving the right pointer left decreases it. This monotonic property guarantees that we don't miss any valid pairs. Each pointer moves at most n times total, so the inner loop is O(n) for each fixed i.
The duplicate skipping works because, in a sorted array, duplicate values are adjacent. After finding a valid triplet, we skip all copies of nums[left] and nums[right] before moving on. For the outer loop, we skip duplicate values of nums[i] for the same reason. This guarantees every triplet in the result is unique.
i from 0 to n-3:nums[i] > 0, break (since the array is sorted, no three positive numbers can sum to zero).nums[i] equals nums[i-1] (avoid duplicate first elements).left = i + 1 and right = n - 1.left < right:sum = nums[i] + nums[left] + nums[right].sum < 0, increment left (need a larger value).sum > 0, decrement right (need a smaller value).sum == 0, add the triplet, then skip duplicates for both left and right.