#C11017. Three Sum Problem

    ID: 40287 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) \) in the array such that their sum is zero, i.e., \( a+b+c=0 \). Two triplets are considered unique if they do not contain exactly the same set of numbers.

Note: The triplets in the output should be sorted in lexicographical order, where each triplet is sorted in non-decreasing order. If no such triplets are found, output nothing.

Additional constraints:

  • The input begins with an integer \( n \) denoting the number of elements in the array.
  • The next line contains \( n \) space-separated integers.

For example, given the array [-1, 0, 1, 2, -1, -4], the unique triplets that sum to zero are:

-1 -1 2
-1 0 1

inputFormat

The first line contains an integer \( n \) representing the number of elements in the array.

The second line contains \( n \) space-separated integers.

outputFormat

Output each unique triplet in a new line. Within each line, print the three integers separated by a single space, in non-decreasing order. The triplets themselves should be printed in lexicographical order. If there are no valid triplets, output nothing.

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

-1 0 1

</p>