#K46507. Four Sum: Unique Quadruplets Summing to Zero

    ID: 27991 Type: Default 1000ms 256MiB

Four Sum: Unique Quadruplets Summing to Zero

Four Sum: Unique Quadruplets Summing to Zero

Given an array of integers, your task is to find all unique quadruplets [a, b, c, d] such that they satisfy the equation (a + b + c + d = 0). The quadruplets must be listed in lexicographical order (i.e. sorted based on their elements), and no duplicate quadruplets are allowed.

Input/Output Format:

  • Input: The first line contains an integer \(n\), which is the number of elements in the array. The second line contains \(n\) space-separated integers.
  • Output: If one or more quadruplets exist, print each quadruplet in a separate line with the four integers separated by a single space. If no quadruplet exists, output [].

Note: Use efficient algorithms as the input size can be large.

inputFormat

The first line contains an integer (n) (the number of elements). The second line contains (n) space-separated integers, which are the elements of the array.

outputFormat

If there are quadruplets [a, b, c, d] such that (a + b + c + d = 0), output each quadruplet in a new line with the four numbers separated by a space. The quadruplets should be printed in lexicographical order. If no such quadruplet exists, output [].## sample

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

-2 0 0 2 -1 0 0 1

</p>