95. Quad Sum
mediumarraytwo pointerssorting
Return every unique quadruplet of values [a, b, c, d] taken from distinct indices of nums with a + b + c + d = target.
Sort each quadruplet ascending and the list of quadruplets lexicographically.
Example 1
Inputnums = [1,0,-1,0,-2,2], target = 0
Output[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2
Inputnums = [2,2,2,2,2], target = 8
Output[[2,2,2,2]]
Constraints
- 1 ≤ nums.length ≤ 200
- -10⁹ ≤ nums[i], target ≤ 10⁹
Hint 1
Sort, fix two indices, then sweep the remaining pair with two pointers.