Find unique triplets that sum up to zero
Given an array, find all unique (no duplicates) triplets of numbers \(a, b, c\) that sum up to zero
\(a + b + c = 0\)
E.g.
{1, 4, -2, 1, 0, -1}
the triplets are
{1, -2, 1} {0, 1, -1}
The additional difficulty from a normal sort-and-2sweep after fixing each \( a \) element comes from the uniqueness of each triplet.
-6 -1 -1 -1 0 1 2
| | |
i j k
It is both necessary to skip \( j \) ‘s next elements after evaluating the solution {-1, -1}
as well as \( i \)’s next elements if we’ve already pivoted a 2sweep with the same number.
The algorithms runs in \( O(N^2) \).