Explorer
Data Structures & Algorithms

Find all unique triplets in the array which gives the sum of zero.

Problem Statement

Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. Notice that the solution set must not contain duplicate triplets.

Examples

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

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

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

Input: nums = [0, 1, 1]

Output: []

Explanation: No matching element or subarray exists, so the output is empty.

Input: nums = [0, 0, 0]

Output: [[0, 0, 0]]

Explanation: Combining the input according to 3 Sum logic yields [[0, 0, 0]].

Complexity

Time Complexity: O(N²)

Space Complexity: O(1)

Hints

💡 Hint 1: Sorting the array upfront allows using a two-pointer approach for each fixed element. 💡 Hint 2: Iterate i from 0 to n - 3. Skip duplicates where nums[i] === nums[i - 1]. 💡 Hint 3: Use two pointers (left = i + 1, right = n - 1) to find pairs summing to -nums[i]. Skip duplicates after finding a match.

Editorial & Approach

Problem Overview & Intuition

Sorting the array in O(N log N) allows solving the remainder with the two-pointer technique in O(N²). For each element nums[i], we search for pairs in the remaining subarray that sum to -nums[i]. Duplicate skipping prevents redundant triplets.

Step-by-Step Approach

  1. Sort nums in ascending order.
  2. Loop i from 0 to nums.length - 3.
  3. If i > 0 && nums[i] === nums[i - 1], skip to prevent duplicates.
  4. Use left = i + 1 and right = nums.length - 1.
  5. While left < right:
  6. If sum === 0, add [nums[i], nums[left], nums[right]], advance both pointers while skipping duplicates.
  7. If sum < 0, left++; if sum > 0, right--.
  8. Return result.

Optimal Implementation (JavaScript)

function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    let left = i + 1, right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], 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 < 0) left++;
      else right--;
    }
  }
  return result;
}

Complexity Analysis

Time Complexity O(N²) — O(N log N) sort + O(N²) two-pointer traversal.
Space Complexity O(1) auxiliary space (excluding output array).

Edge Cases & Corner Traps Handled

  • Array of all zeroes: returns [[0, 0, 0]].
  • No valid triplet sums to 0: returns [].
  • Array length < 3: returns [].

3 Sum

Hard
Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. Notice that the solution set must not contain duplicate triplets.
Example Scenarios
1Example 1
Input: nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]
Explanation:

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

2Example 2
Input: nums = [0, 1, 1]
Output: []
Explanation:

No matching element or subarray exists, so the output is empty.

3Example 3
Input: nums = [0, 0, 0]
Output: [[0, 0, 0]]
Explanation:

Combining the input according to 3 Sum logic yields [[0, 0, 0]].

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