4Sum

Given an integer array and a target, return all unique quadruplets that sum to the target. Sorting plus two nested fixed positions plus two pointers is the standard solution pattern.

Input Format

nums = array of integers, target = required sum

Output Format

list of unique quadruplets

Constraints

  • 4 <= nums.length <= 200; -10^9 <= nums[i], target <= 10^9

Examples

Example 1:

Input:

nums = [1,0,-1,0,-2,2]
target = 0

Output:

[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

Explanation:

These are the unique quadruplets that sum to 0.

Example 2:

Input:

nums = [2,2,2,2,2]
target = 8

Output:

[[2,2,2,2]]

Explanation:

Only one unique quadruplet exists.

Loading...
4Sum - Sorting Based Array Problems DSA Problem