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.
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: -2 + (-1) + 1 + 2 = 0, -2 + 0 + 0 + 2 = 0 and -1 + 0 + 0 + 1 = 0. These are the only distinct quadruplets that reach the target 0.
Example 2
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
Explanation: Any four of the 2s add up to 8. They all form the same quadruplet [2,2,2,2], so it is reported once.
Example 3
Input: nums = [1,2,3,4], target = 100
Output: []
Explanation: The largest possible sum is 1 + 2 + 3 + 4 = 10, which is far below 100, so no quadruplet works.
Sort, fix the first two numbers with nested loops, then two-pointer the remaining pair — the same idea as 3Sum with one more outer loop. Skip duplicates at every level.
Run checks all cases above. Submit evaluates all test cases.