#C14645. Three Sum Problem

    ID: 44317 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 they satisfy ( a + b + c = 0 ). Each triplet must be output in non-decreasing order and the collection of triplets should be sorted in lexicographical order. Note that the solution must not include duplicate triplets.

In this problem, the input is provided via standard input (stdin) and the output must be printed to standard output (stdout).

inputFormat

The first line of input contains a single integer ( n ) representing the number of elements in the array. The second line contains ( n ) space-separated integers.

Example: 6 -1 0 1 2 -1 -4

outputFormat

The first line of output should contain a single integer ( k ) representing the number of unique triplets found. Each of the following ( k ) lines should contain a triplet of integers in non-decreasing order separated by a space.

If no such triplet exists, output 0 on the first line and nothing else.## sample

6
-1 0 1 2 -1 -4
2

-1 -1 2 -1 0 1

</p>