#C13822. Triplets with Sum Zero
Triplets with Sum Zero
Triplets with Sum Zero
Given an array of integers, your task is to find all unique triplets \((a, b, c)\) such that \(a + b + c = 0\). Each triplet must have its elements in non-decreasing order, and the overall list of triplets must be sorted in lexicographical order.
For example, if the input array is [-1, 0, 1, 2, -1, -4]
, the output should be [[-1, -1, 2], [-1, 0, 1]]
.
Solve the problem by reading from standard input and printing the result to standard output.
inputFormat
The first line contains an integer \(n\) indicating the number of elements in the array. The second line contains \(n\) space-separated integers.
outputFormat
Output the list of unique triplets in Python list format. Each triplet is a list of three integers. The overall list must be sorted in lexicographical order. If no such triplet exists, output an empty list []
.
6
-1 0 1 2 -1 -4
[[-1, -1, 2], [-1, 0, 1]]