#K49527. Zero Sum Triplets

    ID: 28662 Type: Default 1000ms 256MiB

Zero Sum Triplets

Zero Sum Triplets

Given an array of integers, your task is to find all unique triplets ( (a, b, c) ) such that their sum is zero, i.e., ( a+b+c=0 ). Each triplet should be printed in non-decreasing order and duplicate triplets must be avoided. This problem requires you to process multiple test cases.

For each test case, you will be given the size of the array followed by the array elements. Your program should output the number of valid triplets and then list each triplet on a separate line. Use an efficient algorithm to handle large arrays within the given constraints.

inputFormat

The input begins with an integer ( T ) representing the number of test cases. Each test case is described below:

  1. An integer ( N ) denoting the number of elements in the array.
  2. A line containing ( N ) space-separated integers that represent the array elements.

outputFormat

For each test case, output the results in the following format:

  • First, print an integer ( M ) which is the number of unique triplets that sum to zero.
  • Then print ( M ) lines, each containing three space-separated integers (in non-decreasing order) representing a valid triplet.

If there are no such triplets for a test case, simply output 0.## sample

4
6
-1 0 1 2 -1 -4
3
0 0 0
5
-2 0 1 1 2
3
1 2 3
2

-1 -1 2 -1 0 1 1 0 0 0 2 -2 0 2 -2 1 1 0

</p>