Explorer
Data Structures & Algorithms

Find all unique quadruplets in the array which gives the sum of target.

Problem Statement

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.

Examples

Input: nums = [1, 0, -1, 0, -2, 2], target = 0

Output: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]

Explanation: Combining the input according to 4 Sum logic yields [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]].

Input: nums = [2, 2, 2, 2, 2], target = 8

Output: [[2, 2, 2, 2]]

Explanation: Combining the input according to 4 Sum logic yields [[2, 2, 2, 2]].

Complexity

Time Complexity: O(N³)

Space Complexity: O(1)

Hints

💡 Hint 1: Sort the array and fix two elements i and j, then use two pointers for the remaining two elements. 💡 Hint 2: Skip duplicate values of nums[i] and nums[j] to avoid duplicate quadruplets. 💡 Hint 3: Check if target sum matches, and advance pointers while skipping duplicate inner values.

Editorial & Approach

Problem Overview & Intuition

By sorting the array, we fix two pointers (i, j) in nested loops and use two pointers (left, right) for the remaining pair. This reduces the search from O(N⁴) brute force to O(N³). Duplicate skipping ensures uniqueness.

Step-by-Step Approach

  1. Sort nums in ascending order.
  2. Outer loop i from 0 to n - 4, skipping duplicates.
  3. Inner loop j from i + 1 to n - 3, skipping duplicates.
  4. Run two pointers left = j + 1, right = n - 1.
  5. When sum === target, record quadruplet and skip inner duplicates.
  6. Return result.

Optimal Implementation (JavaScript)

function fourSum(nums, target) {
  nums.sort((a, b) => a - b);
  const result = [];
  const n = nums.length;
  for (let i = 0; i < n - 3; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    for (let j = i + 1; j < n - 2; j++) {
      if (j > i + 1 && nums[j] === nums[j - 1]) continue;
      let left = j + 1, right = n - 1;
      while (left < right) {
        const sum = nums[i] + nums[j] + nums[left] + nums[right];
        if (sum === target) {
          result.push([nums[i], nums[j], nums[left], nums[right]]);
          while (left < right && nums[left] === nums[left + 1]) left++;
          while (left < right && nums[right] === nums[right - 1]) right--;
          left++;
          right--;
        } else if (sum < target) left++;
        else right--;
      }
    }
  }
  return result;
}

Complexity Analysis

Time Complexity O(N³) — two outer loops and one two-pointer pass.
Space Complexity O(1) auxiliary space (excluding output).

Edge Cases & Corner Traps Handled

  • Length < 4: returns [].
  • Large target values.
  • Array of duplicate numbers.

4 Sum

Hard
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 Scenarios
1Example 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:

Combining the input according to 4 Sum logic yields [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]].

2Example 2
Input: nums = [2, 2, 2, 2, 2], target = 8
Output: [[2, 2, 2, 2]]
Explanation:

Combining the input according to 4 Sum logic yields [[2, 2, 2, 2]].

Editor
Loading Editor...
nums =
[1, 0, -1, 0, -2, 2]
target =
0
Output:Click "Run" above to execute and verify your code here.
[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]