List Every Unique Quadruplet That Sums to a Target
Solve this Problemnums and an integer target, find every combination of four different elements whose values add up to target, and report them with no duplicate quadruplet appearing twice — a quadruplet is a duplicate of another if it has the same four values, regardless of which indices produced them.
Since the answer is checked as a single flat list of numbers, package your result the same way as the triplet version: sort the four values inside each quadruplet from smallest to largest, then arrange the quadruplets themselves from smallest to largest (comparing their first value, then second, then third, then fourth), and concatenate everything into one array.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
4 ≤ nums.length ≤ 200 - ◆
-10⁵ ≤ nums[i] ≤ 10⁵ - ◆
-10⁵ ≤ target ≤ 10⁵
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int[] quadrupletsSummingToTarget(int[] nums, int target) { |
| 3 | Arrays.sort(nums); |
| 4 | List<Integer> result = new ArrayList<>(); |
| 5 | int n = nums.length; |
| 6 | for (int i = 0; i < n - 3; i++) { |
| 7 | if (i > 0 && nums[i] == nums[i - 1]) continue; |
| 8 | for (int j = i + 1; j < n - 2; j++) { |
| 9 | if (j > i + 1 && nums[j] == nums[j - 1]) continue; |
| 10 | int left = j + 1, right = n - 1; |
| 11 | while (left < right) { |
| 12 | int sum = nums[i] + nums[j] + nums[left] + nums[right]; |
| 13 | if (sum == target) { |
| 14 | result.add(nums[i]); |
| 15 | result.add(nums[j]); |
| 16 | result.add(nums[left]); |
| 17 | result.add(nums[right]); |
| 18 | left++; |
| 19 | right--; |
| 20 | while (left < right && nums[left] == nums[left - 1]) left++; |
| 21 | while (left < right && nums[right] == nums[right + 1]) right--; |
| 22 | } else if (sum < target) { |
| 23 | left++; |
| 24 | } else { |
| 25 | right--; |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | int[] flat = new int[result.size()]; |
| 31 | for (int k = 0; k < flat.length; k++) flat[k] = result.get(k); |
| 32 | return flat; |
| 33 | } |
| 34 | } |
| 35 |
nums = [2, -1, 0, 3, -3, 1], target = 2. Sorted, it becomes [-3, -1, 0, 1, 2, 3].
Approach & Solutions
Brute Force — Four Nested Loops
BruteCheck every combination of four distinct indices with four nested loops. Whenever the four values sum to target, sort them and use a string key so the same quadruplet (by value) isn't recorded twice. Sort the collected quadruplets into the required order and flatten them into one array.
O(n⁴)O(n) for the dedupe set1class Solution {
2 public int[] quadrupletsSummingToTarget(int[] nums, int target) {
3 int n = nums.length;
4 Set<String> seen = new HashSet<>();
5 List<int[]> quads = new ArrayList<>();
6 for (int i = 0; i < n; i++) {
7 for (int j = i + 1; j < n; j++) {
8 for (int k = j + 1; k < n; k++) {
9 for (int l = k + 1; l < n; l++) {
10 if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
11 int[] q = {nums[i], nums[j], nums[k], nums[l]};
12 Arrays.sort(q);
13 String key = q[0] + "," + q[1] + "," + q[2] + "," + q[3];
14 if (seen.add(key)) quads.add(q);
15 }
16 }
17 }
18 }
19 }
20 quads.sort((a, b) -> {
21 for (int idx = 0; idx < 4; idx++) if (a[idx] != b[idx]) return a[idx] - b[idx];
22 return 0;
23 });
24 int[] flat = new int[quads.size() * 4];
25 int idx = 0;
26 for (int[] q : quads) for (int v : q) flat[idx++] = v;
27 return flat;
28 }
29}Optimal — Sort + Nested Two Pointer
OptimalSort the array. Fix the first two numbers with two nested loops (each skipping repeats of its own previous value at the same nesting level), then sweep the remainder with two pointers exactly like the triplet version: match records the quadruplet and steps both pointers inward past duplicates, sum too small moves left up, sum too large moves right down.
O(n³)O(1) extra (excluding the sort and output)1class Solution {
2 public int[] quadrupletsSummingToTarget(int[] nums, int target) {
3 Arrays.sort(nums);
4 List<Integer> result = new ArrayList<>();
5 int n = nums.length;
6 for (int i = 0; i < n - 3; i++) {
7 if (i > 0 && nums[i] == nums[i - 1]) continue;
8 for (int j = i + 1; j < n - 2; j++) {
9 if (j > i + 1 && nums[j] == nums[j - 1]) continue;
10 int left = j + 1, right = n - 1;
11 while (left < right) {
12 int sum = nums[i] + nums[j] + nums[left] + nums[right];
13 if (sum == target) {
14 result.add(nums[i]);
15 result.add(nums[j]);
16 result.add(nums[left]);
17 result.add(nums[right]);
18 left++;
19 right--;
20 while (left < right && nums[left] == nums[left - 1]) left++;
21 while (left < right && nums[right] == nums[right + 1]) right--;
22 } else if (sum < target) {
23 left++;
24 } else {
25 right--;
26 }
27 }
28 }
29 }
30 int[] flat = new int[result.size()];
31 for (int k = 0; k < flat.length; k++) flat[k] = result.get(k);
32 return flat;
33 }
34}