Find every set of three values adding up to 0, without repeats. The array is sorted. Fix an anchor, then run two-sum on everything to its right.
Looking for every triplet summing to 0 in a sorted array, one anchor at a time.
Anchor -4 at index 0. Left and right start at the ends of everything to its right.
The anchor is -4 at index 0, with left at index 1 and right at index 5.
-4 + -1 + 2 = -3, short of 0. Only a bigger left value can help, so left moves up.
-4 plus -1 plus 2 is -3. Less than the target, so the left pointer moves to index 2.
-4 + -1 + 2 = -3, short of 0. Only a bigger left value can help, so left moves up.
-4 plus -1 plus 2 is -3. Less than the target, so the left pointer moves to index 3.
-4 + 0 + 2 = -2, short of 0. Only a bigger left value can help, so left moves up.
-4 plus 0 plus 2 is -2. Less than the target, so the left pointer moves to index 4.
-4 + 1 + 2 = -1, short of 0. Only a bigger left value can help, so left moves up.
-4 plus 1 plus 2 is -1. Less than the target, so the left pointer moves to index 5.
Anchor moves to -1 at index 1. Everything before it is done. Left and right reset to the ends of the rest.
The anchor is -1 at index 1, with left at index 2 and right at index 5.
-1 + -1 + 2 = 0. That is a triplet: (-1, -1, 2). Move both pointers inward.
-1 plus -1 plus 2 is 0. That is a triplet. Both pointers move inward.
-1 + 0 + 1 = 0. That is a triplet: (-1, 0, 1). Move both pointers inward.
-1 plus 0 plus 1 is 0. That is a triplet. Both pointers move inward.
Anchor -1 at index 2 is the same value as the previous anchor, so any triplet it starts was already found from there. Skip it.
The anchor -1 repeats the previous anchor and is skipped.
Anchor moves to 0 at index 3. Everything before it is done. Left and right reset to the ends of the rest.
The anchor is 0 at index 3, with left at index 4 and right at index 5.
0 + 1 + 2 = 3, past 0. Only a smaller right value can help, so right moves down.
0 plus 1 plus 2 is 3. More than the target, so the right pointer moves to index 4.
2 triplets: (-1, -1, 2) (-1, 0, 1). Each anchor ran a linear two-sum, so O(n^2) in all, with no duplicates to filter out.
2 triplets found: (-1, -1, 2) (-1, 0, 1).