#C14358. Zero Sum Triplets
Zero Sum Triplets
Zero Sum Triplets
Given an integer array of size \(n\), find all unique triplets \(\{a, b, c\}\) such that \(a+b+c=0\). Each triplet should be output in non-decreasing order, and the overall list of triplets must be sorted in lexicographical order. Duplicate triplets should not appear in the output.
The input is read from standard input. The first line contains an integer \(n\) representing the number of elements in the array, and the second line contains \(n\) space-separated integers. The output should be a JSON-like array of arrays representing the triplets. If no such triplet exists, output an empty array: []
.
inputFormat
The first line of input contains a single integer \(n\) (the number of elements). The second line contains \(n\) space-separated integers.
outputFormat
Output a JSON-like array of arrays where each inner array represents a triplet \([a, b, c]\) summing to zero. The triplets and the numbers within each triplet must be in non-decreasing order. If no valid triplet exists, output []
.
6
-1 0 1 2 -1 -4
[[-1, -1, 2], [-1, 0, 1]]