Example 1
Input
four_sum([1, 0, -1, 0, -2, 2], 0)
Output
[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
These are the unique quadruplets summing to 0, sorted lexicographically.
Full lesson preview
Return all unique 4-number combinations from an array that add up to a target value. Use sorting, two-pointer scanning and deduplication.
Problem statement
Task
Examples
Input
four_sum([1, 0, -1, 0, -2, 2], 0)
Output
[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
These are the unique quadruplets summing to 0, sorted lexicographically.
Input format
Output format
Constraints
Samples
Input
[1, 0, -1, 0, -2, 2], target=0
Output
[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
Classic example producing three unique quadruplets.