#C13024. Four Sum Problem
Four Sum Problem
Four Sum Problem
You are given an array of integers \(nums\) and an integer \(target\). Your task is to find all unique quadruplets \([a, b, c, d]\) in the array such that:
\(a + b + c + d = target\)
Each quadruplet should be listed in non‐decreasing order, and the solution set must not contain duplicate quadruplets. This is a classic problem that can be solved efficiently using sorting and the two‐pointers technique.
Note: Use stdin to receive the input and stdout to print the result.
inputFormat
The input is read from stdin
and consists of three lines:
- An integer \(n\) representing the number of elements in the array.
- \(n\) space-separated integers representing the array \(nums\).
- An integer \(target\), the target sum.
For example:
6 1 0 -1 0 -2 2 0
outputFormat
The output should be printed to stdout
as a list (in Python list format) of quadruplets. Each quadruplet is represented as a list of four integers in non-decreasing order. If no quadruplets exist, print an empty list []
.
For example:
[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]## sample
6
1 0 -1 0 -2 2
0
[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]