3Sum

Two Sum was about finding a pair. 3Sum asks for ALL triplets that sum to zero. Seems like a natural extension — but there is a trap hiding in the word “all.”

Try it yourself. Tap three numbers to form a triplet, and see what happens when the same values keep coming back from different places in the array.

Find the Triplets

Given -2-10012, find all triplets that sum to 0. Tap three cells to check a triplet.

FIG. 1 — TRIPLET TAPPING
0
1
2
3
4
5
Target sum: 0Tap three cells
— every index combination, every sum —