AlgoLounge

4Sum

medium

Given an array of integers nums and an integer target, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

  • 0 <= a, b, c, d < n
  • a, b, c, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target

You may return the answer in any order.

Examples

Example 1

Input:  nums = [3,2,3,-3,1,0], target = 3
Output: [[-3,0,3,3],[-3,1,2,3]]

Example 2

Input:  nums = [1,-1,1,-1,1,-1], target = 2
Output: [[-1,1,1,1]]

Example 3

Input:  nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

Constraints

  • 1 <= nums.length <= 200
  • -1,000,000,000 <= nums[i] <= 1,000,000,000
  • -1,000,000,000 <= target <= 1,000,000,000
TypeScript
Loading editor…
List of test cases (10)
Case 1
nums = [3,2,3,-3,1,0]
target = 3
Case 2
nums = [1,-1,1,-1,1,-1]
target = 2
Case 3
nums = [1,0,-1,0,-2,2]
target = 0
Case 4
nums = [2,2,2,2,2]
target = 8
Case 5
nums = [1,2,3,4]
target = 10
Case 6
nums = [-3,-2,-1,0,0,1,2,3]
target = 0
Case 7
nums = [1,1,1,1]
target = 4
Case 8
nums = [0]
target = 0
Case 9
nums = [1,-2,-5,-4,-3,3,3,5]
target = -11
Case 10
nums = [1000000000,1000000000,1000000000,1000000000]
target = -294967268