Problem
Given an integer array, return all unique triplets [a, b, c] such that the three values are at different indices and a + b + c = 0. The triplets may be returned in any order, but each unique combination of values should appear once.
Original: LeetCode 15: 3Sum.
Examples
Example 1
`nums = [-1, 0, 1, 2, -1, -4]``[[-1, -1, 2], [-1, 0, 1]]`Example 2
`nums = [0, 1, 1]``[]`Example 3
`nums = [0, 0, 0]``[[0, 0, 0]]`Constraints
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5
Hints
Hint 1
"Unique" refers to the values, not the indices: [-1,0,1] should appear once even if -1 occurs twice in positions that could form it more than one way, while [-1,-1,2] is a different triplet and must be kept when two -1 values exist.
Hint 2
k-Sum: sort, fix the first k−2 values, and solve the rest with opposite-ends two pointers. For k = 3 the inner step is a Two Sum II.
Solution
Tried it yourself? The approach, complexity, and code stay hidden until you choose.
Approach
Brute force. Try every triple and store it in a set of sorted tuples: O(n³) time and extra hashing cost.
Optimal. Sort. For each index i, skip it if it is the same value as i−1, then run two pointers on the suffix for a pair that sums to -nums[i]. When a pair hits the target, record it and skip duplicate values on both sides. A suffix that cannot reach zero (the smallest two already overshoot, or the largest two already fall short) can exit early, but the skip logic is the part interviewers watch for.
Alternative. For each i, solve Two Sum on the suffix with a hash set. That is also O(n²), but the set uses extra memory and makes de-duplication messier than the sorted two-pointer version.
Complexity
- Time
- O(n²)
O(n²) time after an O(n log n) sort. O(1) extra space besides the output, if you sort in place.
- Space
- O(1)
Code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ThreeSum {
static List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
var ans = new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int lo = i + 1, hi = nums.length - 1;
while (lo < hi) {
int sum = nums[i] + nums[lo] + nums[hi];
if (sum == 0) {
ans.add(List.of(nums[i], nums[lo], nums[hi]));
lo++;
hi--;
while (lo < hi && nums[lo] == nums[lo - 1]) lo++;
while (lo < hi && nums[hi] == nums[hi + 1]) hi--;
} else if (sum < 0) lo++;
else hi--;
}
}
return ans;
}
public static void main(String[] args) {
System.out.println(threeSum(new int[] {-1, 0, 1, 2, -1, -4})); // [[-1, -1, 2], [-1, 0, 1]]
System.out.println(threeSum(new int[] {0, 1, 1})); // []
System.out.println(threeSum(new int[] {0, 0, 0})); // [[0, 0, 0]]
}
}Edge cases
- All zeros, which should return [[0,0,0]] once.
- Fewer than three values, or no triplet that sums to zero, as in [0,1,1].
- A mix of duplicates that still forms more than one distinct triplet, as in [-1,0,1,2,-1,-4].
Follow-ups
- 3Sum closest, or 4Sum: the same pattern with one more loop.
- This problem is regularly named in public Microsoft interview reports.
Interview script
"I sort, then fix one number and use two pointers on the rest to find a pair that sums to its negation, which is the same idea as Two Sum II. I skip duplicates at the fixed index and on both pointers so each triplet is unique. After the sort, that's O(n²)."
Source: LeetCode 15: 3Sum Source: Java SE 17 API: Arrays.sort) Source: LeetCode Discuss: Microsoft SDE-2 recent questions 2025 (candidate reports, secondary)
Sources
- LeetCode 167: Two Sum II - Input Array Is Sorted
- LeetCode 11: Container With Most Water
- Java SE 17 API: Math.min)
- LeetCode 42: Trapping Rain Water
- LeetCode 88: Merge Sorted Array
- LeetCode 283: Move Zeroes
- LeetCode 26: Remove Duplicates from Sorted Array
- Java SE 17 API: Arrays.copyOf)
- LeetCode 392: Is Subsequence
- LeetCode 15: 3Sum
- Java SE 17 API: Arrays.sort)
- LeetCode Discuss: Microsoft SDE-2 recent questions 2025 (candidate reports, secondary)
- LeetCode 125: Valid Palindrome
- Java SE 17 API: Character.isLetterOrDigit)
- LeetCode 75: Sort Colors
Sources (3)
- LeetCode 15: 3Sumleetcode.com
- Java SE 17 API: Arrays.sortdocs.oracle.com
- LeetCode Discuss: Microsoft SDE-2 recent questions 2025 (candidate reports, secondary)leetcode.com