#K35177. Four Sum Problem
Four Sum Problem
Four Sum Problem
Given an array of integers and a target value \(T\), find all unique quadruplets \( [a, b, c, d] \) in the array such that:
\( a + b + c + d = T \)
The quadruplets must be unique (the solution set must not contain duplicate quadruplets), and each quadruplet should be printed in non-decreasing order.
Example:
Input: [1, 0, -1, 0, -2, 2], T = 0 Output: -2 -1 1 2 -2 0 0 2 -1 0 0 1
If no quadruplet exists, output []
.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains space-separated integers representing the array.
- The second line contains a single integer, the target value \(T\).
outputFormat
Output all unique quadruplets that sum to \(T\) to standard output (stdout). Each quadruplet should be printed on a separate line with its numbers separated by a single space. If no quadruplet is found, output a single line containing []
.
1 0 -1 0 -2 2
0
-2 -1 1 2
-2 0 0 2
-1 0 0 1
</p>