#C7972. Three Sum: Finding All Unique Triplets with Zero Sum

    ID: 51902 Type: Default 1000ms 256MiB

Three Sum: Finding All Unique Triplets with Zero Sum

Three Sum: Finding All Unique Triplets with Zero Sum

Given an array of integers, your task is to find all unique triplets in the array such that the sum of the triplet is zero. In other words, you must find all distinct groups \( (a, b, c) \) where

[ a+b+c=0 ]

Each triplet should be printed in non-decreasing order and the overall list of triplets should be sorted lexicographically. If no such triplets exist, output [].

Note: The input is provided via stdin and the output should be written to stdout. The first line contains an integer \( n \) denoting the number of elements, followed by a line with \( n \) space-separated integers.

inputFormat

The first line of input contains an integer \( n \), indicating the number of elements in the array. The second line contains \( n \) space-separated integers.

outputFormat

If at least one triplet exists, output each unique triplet on a new line with the three integers separated by a single space. The integers within each triplet should be in non-decreasing order, and the triplets themselves should be sorted lexicographically. If no such triplet exists, output [] on a single line.

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

-1 0 1

</p>