3Sum
The drill: Find every unique triple of values that sums to zero — value-triples, not index-triples, so the same numbers in a different order never count twice.
An array of integers arrives, and the task is to surface every combination of three distinct positions whose values add up to zero.
What matters is the triple of values, not which positions produced them — two triples with the same three numbers, even from different index combinations, count as one and only one answer.
The order of numbers within a triple, and the order triples appear in the result, doesn't matter for correctness; only the set of distinct value-triples found matters.
- array length runs from zero up to a few thousand elements
- values can be negative, zero, or positive, with duplicates common
- each triple must use three different positions in the array
- duplicate value-triples must be collapsed to one entry each
HINT 1 THE NUDGE
Cubic tries every triple and then fights duplicates on top. Sorting first makes both problems easier at once.
HINT 2 THE STRUCTURE
Fix the smallest element of the triple; what remains is two-sum on a sorted array — two pointers walking inward.
HINT 3 ONE STEP FROM THE ANSWER
Skip equal neighbours when advancing the anchor AND after each found pair — that is where the dedup lives. Sum too small → left pointer right; too big → right pointer left.
Sorted the array: [-4, -1, -1, 0, 1, 2]. Fix the smallest value as anchor, close the rest with two pointers.
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
out = []
n = len(nums)
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
lo, hi = i + 1, n - 1
while lo < hi:
s = nums[i] + nums[lo] + nums[hi]
if s < 0:
lo += 1
elif s > 0:
hi -= 1
else:
out.append([nums[i], nums[lo], nums[hi]])
lo += 1
while lo < hi and nums[lo] == nums[lo - 1]:
lo += 1
hi -= 1
return outclass Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
found = set()
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if nums[i] + nums[j] + nums[k] == 0:
found.add(tuple(sorted((nums[i], nums[j], nums[k]))))
return [list(t) for t in found]class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> out = new ArrayList<>();
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int lo = i + 1;
int hi = nums.length - 1;
while (lo < hi) {
int s = nums[i] + nums[lo] + nums[hi];
if (s < 0) {
lo++;
} else if (s > 0) {
hi--;
} else {
out.add(List.of(nums[i], nums[lo], nums[hi]));
lo++;
while (lo < hi && nums[lo] == nums[lo - 1]) {
lo++;
}
hi--;
}
}
}
return out;
}
}class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> found = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
List<Integer> t = new ArrayList<>(List.of(nums[i], nums[j], nums[k]));
Collections.sort(t);
found.add(t);
}
}
}
}
return new ArrayList<>(found);
}
}✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED