Medium · Sort + Two Pointers
Understand → Visualise → Practice.
Return all unique value triplets whose sum is zero; triplet and result order do not matter.
Sort the array. For each first value, find pairs in the rest that sum to its negative with two pointers, skipping duplicates.
Sorting enables two pointers and duplicate skipping.
Skip it if it equals the previous first value.
Move the pointers by comparing the sum with 0; after a hit, skip equal values at both pointers.
O(n²) time and O(1) auxiliary space apart from output.
Skipping duplicates only for the fixed value still emits duplicate triplets from the pointers.
Read the full guide first: Two Pointers and Sliding Windows.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.