Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get an integer array nums. Return every triplet [a, b, c] taken from three different positions where a + b + c = 0. The list must not contain the same triplet twice, even if the array has repeated values. The order of the triplets, and the order inside each triplet, doesn't matter.
Constraints that matter: 3 to 3000 numbers, each between −10⁵ and 10⁵.
Example: nums = [-1, 0, 1, 2, -1, -4] → [[-1, -1, 2], [-1, 0, 1]]. There are two -1s, so [-1, 0, 1] can be built two ways, but it appears only once in the answer.
Start with brute force
Three nested loops try every i < j < k. To remove duplicates, sort each found triplet and put it in a set.
That's about n³/6 checks. For n = 3000 that's 4.5·10⁹, which is far too slow. On top of that, the set trick for duplicates is clumsy. We want an approach that never produces a duplicate in the first place.
Building the intuition
Three unknowns is hard. Can you make it two?
Fix one number. Say the triplet's first number is x = nums[i]. Now you need two other numbers that sum to −x. That's Two Sum. And if the array is sorted, it's Two Sum II, which you can solve in one O(n) sweep with two pointers and no extra memory.
So: sort, then for each i, run Two Sum II on everything to the right of i. That's n sweeps of O(n), so O(n²) in total. That's a huge win over n³.
Sorting gives you a second gift, which is how you handle duplicates. In a sorted array, equal values sit next to each other. Look at [-4, -1, -1, 0, 1, 2]. When i is on the first -1, the sweep finds every triplet that starts with -1. When i moves to the second -1, it would find the same triplets again. So the rule is: if nums[i] == nums[i - 1], skip this i.
The same thing happens inside the sweep. After you find a triplet, if the next left holds the same value, it would produce the same triplet. So keep stepping left while it equals the value you just used.
The optimal approach
- Sort
nums. - For each
ifrom 0 to n − 3:- If
nums[i] > 0, stop. The array is sorted, so all three numbers would be positive and can't sum to 0. - If
i > 0andnums[i] == nums[i − 1], skip it. Those triplets are already found. - Set
left = i + 1andright = n − 1. Whileleft < right:total = nums[i] + nums[left] + nums[right];- if
total < 0, doleft += 1; iftotal > 0, doright -= 1; - if
total == 0, record the triplet, move both pointers inwards, then keep movingleftwhilenums[left] == nums[left − 1].
- If
- Return the list.
The subtle detail: after a hit, you only need to skip duplicates on the left side. Once left is on a new value, the old right value can't complete a triplet with it: nums[i] is fixed and nums[left] changed, so the partner needed changed too. The normal "too big, move right" rule moves right off the old value on its own.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort() # sorting lets two pointers work and puts duplicates side by side
n = len(nums)
result = []
for i in range(n - 2):
if nums[i] > 0:
break # the smallest of the three is positive: no sum can be 0
if i > 0 and nums[i] == nums[i - 1]:
continue # same first number as last time: same triplets again
# Two Sum II on the part to the right of i, looking for -nums[i]
left, right = i + 1, n - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total < 0:
left += 1
elif total > 0:
right -= 1
else:
result.append([nums[i], nums[left], nums[right]])
left += 1
right -= 1
# skip copies of the middle number so this triplet isn't added twice
while left < right and nums[left] == nums[left - 1]:
left += 1
return resultclass Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end()); // two pointers need order; duplicates end up side by side
int n = nums.size();
vector<vector<int>> result;
for (int i = 0; i < n - 2; i++) {
if (nums[i] > 0) break; // smallest is positive: no sum can be 0
if (i > 0 && nums[i] == nums[i - 1]) continue; // same first number: same triplets again
// Two Sum II on the part to the right of i, looking for -nums[i]
int left = i + 1, right = n - 1;
while (left < right) {
int total = nums[i] + nums[left] + nums[right];
if (total < 0) {
left++;
} else if (total > 0) {
right--;
} else {
result.push_back({nums[i], nums[left], nums[right]});
left++;
right--;
// skip copies of the middle number so this triplet isn't added twice
while (left < right && nums[left] == nums[left - 1]) left++;
}
}
}
return result;
}
};class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums); // two pointers need order; duplicates end up side by side
int n = nums.length;
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < n - 2; i++) {
if (nums[i] > 0) break; // smallest is positive: no sum can be 0
if (i > 0 && nums[i] == nums[i - 1]) continue; // same first number: same triplets again
// Two Sum II on the part to the right of i, looking for -nums[i]
int left = i + 1, right = n - 1;
while (left < right) {
int total = nums[i] + nums[left] + nums[right];
if (total < 0) {
left++;
} else if (total > 0) {
right--;
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
left++;
right--;
// skip copies of the middle number so this triplet isn't added twice
while (left < right && nums[left] == nums[left - 1]) left++;
}
}
}
return result;
}
}/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
nums.sort((a, b) => a - b); // numeric sort: the default sort compares as strings
const n = nums.length;
const result = [];
for (let i = 0; i < n - 2; i++) {
if (nums[i] > 0) break; // smallest is positive: no sum can be 0
if (i > 0 && nums[i] === nums[i - 1]) continue; // same first number: same triplets again
// Two Sum II on the part to the right of i, looking for -nums[i]
let left = i + 1, right = n - 1;
while (left < right) {
const total = nums[i] + nums[left] + nums[right];
if (total < 0) {
left++;
} else if (total > 0) {
right--;
} else {
result.push([nums[i], nums[left], nums[right]]);
left++;
right--;
// skip copies of the middle number so this triplet isn't added twice
while (left < right && nums[left] === nums[left - 1]) left++;
}
}
}
return result;
};Dry run
nums = [-1, 0, 1, 2, -1, -4]. After sorting: [-4, -1, -1, 0, 1, 2] (indices 0–5).
| i | nums[i] | left, right | total | Action |
|---|---|---|---|---|
| 0 | −4 | 1, 5 | −4 − 1 + 2 = −3 | too small, left → 2 |
| 0 | −4 | 2, 5 | −3 | left → 3 |
| 0 | −4 | 3, 5 | −2 | left → 4 |
| 0 | −4 | 4, 5 | −1 | left → 5, the pointers meet |
| 1 | −1 | 2, 5 | −1 − 1 + 2 = 0 | record [-1, -1, 2], move to 3, 4 |
| 1 | −1 | 3, 4 | −1 + 0 + 1 = 0 | record [-1, 0, 1], move to 4, 3 and stop |
| 2 | −1 | — | — | same as nums[1], skip |
| 3 | 0 | 4, 5 | 0 + 1 + 2 = 3 | too big, right → 4, stop |
The loop ends after i = 3, since i only goes up to n − 3. Two triplets, and no duplicates.
Complexity
- Time: O(n²). The sort is O(n log n). Then each of the n values of
iruns one O(n) two-pointer sweep. The n² term dominates. For n = 3000 that's around 4.5·10⁶ steps. - Space: O(1) extra, apart from the output and whatever the sort uses internally (O(log n) for typical in-place sorts).
Edge cases and traps
- All zeros.
[0, 0, 0, 0]→[[0, 0, 0]], just once. Both duplicate skips are needed for this. - No answer.
[0, 1, 1]→[]. Return an empty list, notnull. - Skipping with
nums[i] == nums[i + 1]. That's the wrong neighbour. It would skip the first-1and lose[-1, -1, 2]. Compare with the previous value. - JavaScript's default sort.
[-1, -4, 2].sort()sorts as strings. Always pass(a, b) => a - b. - Early break.
nums[i] > 0is a valid stop.nums[i] >= 0is not, because it would lose[0, 0, 0].
Say it in the interview
"Brute force is O(n³). If I fix the first number, I need two others summing to its negative, and on a sorted array that's Two Sum II with two pointers. So I sort, then for each index I sweep two pointers over the rest, which is O(n²) total. Sorting also puts duplicates next to each other. I skip an index whose value equals the previous one, and after finding a triplet I move left past equal values, so every triplet appears once. I can also stop as soon as the fixed number is positive."
Follow-ups interviewers ask
- "3Sum Closest: the triplet whose sum is closest to a target?" Same sweep. Track the best
|total − target|, and move pointers by comparingtotalwithtarget. - "4Sum?" Fix two numbers with two loops, then two pointers: O(n³). In general, kSum is k − 2 loops around Two Sum II.
- "Can you do it with a hash set instead of sorting?" Yes. For each
i, run Two Sum with a set on the rest. It's also O(n²), but avoiding duplicates gets messier and it uses O(n) memory.
Practise the same idea
This is Day 21 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free