3Sum

MediumTwo Pointers
Next
ArrayTwo PointersSorting

Find all unique triplets in the array that add up to zero. The triplets must not repeat, even if the values appear several times in the input.

Input

Line 1: n. Line 2: n integers.

Output

Print each triplet on its own line as three values in ascending order. List the triplets in ascending lexicographic order. If there are none, print No triplets.

Constraints

3 <= n <= 1500 -10^5 <= each value <= 10^5

Example 1
Input
6
-1 0 1 2 -1 -4
Output
-1 -1 2
-1 0 1

The unique triplets are [-1, -1, 2] and [-1, 0, 1].

Example 2
Input
3
1 2 3
Output
No triplets

No three numbers add up to zero.

Hints

3Sum - Medium Two Pointers Problem | Skill Shiksha