visual walkthrough
3Sum
Reduces k-sum to 2-sum and tests duplicate handling; asked constantly.
The idea
Sort the array and fix the first number. Now the other two must add up to its opposite, which is the Two Sum II problem: one pointer at each end, moving toward whichever side fixes the total.
Skipping repeated values for the fixed number and for the pointers keeps every triplet unique.
Complexity
| approach | time | space |
|---|---|---|
| Three loops | O(n³) | O(1) |
| Sort + two pointers | O(n²) | O(1) |