dsa · medium
Four Sum
Return every unique quadruplet of indices whose values sum to target. Two quadruplets that contain the same four values (ignoring order) are the same and must appear once. Order of the quadruplets does not matter.
Arguments
nums— array of integerstarget— required four-value sum
Example
nums = [1,0,-1,0,-2,2], target = 0 → [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]].
Constraints
1 <= nums.length <= 200 -10^9 <= nums[i], target <= 10^9 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,0,-1,0,-2,2] 0 Expected: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2
Input: [2,2,2,2,2] 8 Expected: [[2,2,2,2]]