#K74452. Three Sum Problem

    ID: 34200 Type: Default 1000ms 256MiB

Three Sum Problem

Three Sum Problem

You are given an array of integers. Your task is to find all unique triplets \( (a, b, c) \) such that they satisfy the equation \(a + b + c = 0\). Each triplet should be output in non-decreasing order (i.e. \(a \le b \le c\)) and the list of triplets should be sorted in lexicographical order. Duplicate triplets are not allowed.

Input: The input is given via standard input (stdin). The first line contains an integer \(n\), representing the number of elements. The second line contains \(n\) space-separated integers, which are the elements of the array.

Output: For each unique triplet found, print the three integers separated by a space on a single line. If no such triplet exists, print nothing.

Note: Use an efficient approach such as sorting combined with the two-pointer method.

inputFormat

The first line contains a single integer \(n\) \( (0 \le n \le 10^4)\), denoting the number of elements in the array. The second line contains \(n\) space-separated integers, each representing an element of the array.

outputFormat

For every unique triplet \((a, b, c)\) that sums to zero, output a line with the three numbers separated by a space in non-decreasing order. The triplets themselves should be sorted lexicographically. If there is no such triplet, output nothing.

## sample
6
-1 0 1 2 -1 -4
-1 -1 2

-1 0 1

</p>