#K54537. Unique Triplets Summing to Zero
Unique Triplets Summing to Zero
Unique Triplets Summing to Zero
You are given an array of integers. Your task is to find all unique triplets in the array which gives the sum of zero.
More formally, given an integer array \(A\), find all unique triplets \((A_i, A_j, A_k)\) such that \(A_i + A_j + A_k = 0\) and \(i \lt j \lt k\). The triplets in the solution set must be sorted in non-descending order and no duplicate triplets are allowed.
This is a classic problem that is often solved using sorting and the two-pointer technique.
inputFormat
The first line of the input contains an integer \(T\) representing the number of test cases. Each test case is described as follows:
- The first line contains an integer \(N\), the length of the array.
- The second line contains \(N\) space-separated integers representing the elements of the array.
All input is read from stdin
.
outputFormat
For each test case, output a single line displaying the list of unique triplets that sum to zero. The result should be formatted as a Python-style list of lists.
If no such triplet exists, print an empty list: []
.
All output should be written to stdout
.
1
6
-1 0 1 2 -1 -4
[[-1, -1, 2], [-1, 0, 1]]
</p>