4Sum - Problem
Given an array nums of n integers, 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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,0,-1,0,-2,2], target = 0
›
Output:
[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
💡 Note:
Three unique quadruplets sum to 0: [-2,-1,1,2], [-2,0,0,2], and [-1,0,0,1]
Example 2 — No Solution
$
Input:
nums = [2,2,2,2,2], target = 8
›
Output:
[[2,2,2,2]]
💡 Note:
Only one unique quadruplet: [2,2,2,2] with sum = 8
Example 3 — Empty Result
$
Input:
nums = [1,2,3,4], target = 20
›
Output:
[]
💡 Note:
No four numbers can sum to 20, maximum possible is 1+2+3+4=10
Constraints
- 1 ≤ nums.length ≤ 200
- -109 ≤ nums[i] ≤ 109
- -109 ≤ target ≤ 109
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code