AlgoScope

Three Sum

Fix one element, run two-sum on the rest. Skip duplicates.

Two pointers, algorithm, intermediate
-4 plus -1 plus 2 is -3. Less than the target, so the left pointer moves to index 2.active 2-5-40-11-12031425ileftright
-4 plus -1 plus 2 is -3. Less than the target, so the left pointer moves to index 2.active 2-5-40-11-12031425ileftright

Two Pointer Problems: Three sum to zero

Decision, step 3 of 13

-4 + -1 + 2 = -3, short of 0. Only a bigger left value can help, so left moves up.

How two pointer problems works

Cost

Best
O(n^2)
Average
O(n^2)
Worst
O(n^2)
Space
O(1)

Taught by the same lesson

Two Pointer Problems covers these too, in the same run.