1. Problem Statement (Simple Explanation):
You are given:
An integer array nums of length n
An integer target
You must find all unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < n
a, b, c, d are all distinct indices
nums[a] + nums[b] + nums[c] + nums[d] == target
Return:
A list of unique quadruplets (order in output doesn’t matter).
No duplicate quadruplets (same four values in possibly different order) should appear in the result.
2. Examples:
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]]
Valid quadruplets that sum to 0:
-2 + -1 + 1 + 2 = 0
-2 + 0 + 0 + 2 = 0
-1 + 0 + 0 + 1 = 0
After removing duplicates and sorting elements within each quadruplet, we get the three unique quadruplets above.
Example 2:
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
Only one quadruplet exists: 2 + 2 + 2 + 2 = 8.
Constraints:
1<=nums.length<=2001
-109 <= nums[i] <= 109
-109 <= target <= 109
3. Approach 1 – Brute Force (O(n⁴), Not Practical):
Conceptually:
Use 4 nested loops to try all combinations (a, b, c, d).
Check if their sum equals target.
Use a set to avoid duplicates.
This is:
Time: O(n4) → too slow for n up to 200.
Space: high if using a set of quadruplets.
So we need a better approach.
4. Approach 2 – Sort + Two Pointers + Pruning (O(n³), Recommended):
This is a generalization of the 3Sum two-pointer pattern.
High-Level Idea:
Sort the array nums.
Fix the first two indices i and j using two nested loops.
For each pair (i, j):
We reduce the problem to 2Sum with target:
newTarget = target - nums[i] - nums[j]
Then perform a standard two-pointer 2Sum on the subarray from j+1 to n-1 to find pairs (left, right) whose sum equals newTarget.
Skip duplicates for i, j, left, and right so we don’t add the same quadruplet multiple times.
Optionally apply pruning using bounds (min/max possible sums) to break loops early, but for n <= 200, the straightforward O(n3) with duplicate skipping is acceptable.
Detailed Algorithm:
Sort nums.
Initialize result as an empty list.
Let n = len(nums).
For i from 0 to n - 4:
If i > 0 and nums[i] == nums[i-1], skip (to avoid duplicate first element).
For j from i + 1 to n - 3:
If j > i + 1 and nums[j] == nums[j-1], skip (to avoid duplicate second element).
Set:
left = j + 1
right = n - 1
twoSumTarget = target - nums[i] - nums[j]
While left < right:
twoSum = nums[left] + nums[right]
if twoSum == twoSumTarget:
Add quadruplet [nums[i], nums[j], nums[left], nums[right]] to result.
Move left forward while nums[left] == nums[left+1] to skip duplicates.
Move right backward while nums[right] == nums[right-1] to skip duplicates.
Then left++, right--.
else if twoSum < twoSumTarget:
left++ (we need a bigger sum).
else:
right-- (we need a smaller sum).
Return result.
Pseudo-code:
Complexity:
Let n = len(nums)
Sorting: O(n*logn)
Outer loops: i and j each up to O(n)
Inner two-pointer per (i, j): O(n)
Total:
Time: O(n3)
Space: O(1) extra (ignoring output, sort in-place)
For n <= 200, O(n3) is acceptable.
5. Java code:
Note: used long for intermediate sums to avoid overflow when nums/target are near 109.
6. C code:
7. C++ code:
8. Python code:
9. JavaScript code:






Comments
Post a Comment