3sum - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

Problem

Return all unique triplets (a, b, c) in an integer array such that a + b + c = 0.

Why Sort First?

Sorting enables a clean two-pointer sweep on the suffix after fixing a first value. In a sorted array: moving the leftIndex right increases the sum, and moving the rightIndex left decreases the sum. This monotonic control makes the search efficient.

Pointers

  • firstIndex — fixed first element of the triplet (outer loop).
  • leftIndex — starts at firstIndex + 1, moves right to increase the sum.
  • rightIndex — starts at the end, moves left to decrease the sum.

Algorithm

  1. Sort the array ascending.
  2. For each firstIndex:
    • Skip duplicate first values: if nums[firstIndex] === nums[firstIndex-1], continue.
      We’ve already used this value as the first number and explored all pairs while its duplicates were available as second/third numbers. Reusing it would only recreate triplets without yielding any unexplored ones.
    • Set two pointers: leftIndex = firstIndex+1, rightIndex = n-1.
    • While leftIndex < rightIndex:
      • Compute total = nums[firstIndex] + nums[leftIndex] + nums[rightIndex].
      • If total === 0:
        • Record the triplet.
        • Move to next fresh leftIndex and rightIndex to shrink the window while avoiding duplicates:
          • First, skip duplicates by advancing leftIndex to the last occurrence of its current value and retreating rightIndex to the first occurrence of its current value.
          • Then, move both inward — do leftIndex++ and rightIndex-- — to shrink the window and continue on new values.
          We’ve already explored all pairs using these duplicate values as second/third; keeping them would regenerate the same triplet without yielding any unexplored ones.
      • If total < 0: the sum is too small. Increase the sum by moving leftIndex one step right (leftIndex++).
      • If total > 0: the sum is too large. Decrease the sum by moving rightIndex one step left (rightIndex--).

Correctness Intuition

  • Sorting gives a monotone relationship: moving leftIndex right increases the sum; moving rightIndex left decreases it.
  • For each fixed first value, the two-pointer sweep explores all pairs in the suffix that could complete zero.
  • De-duplication for the first value and for leftIndex/rightIndex after a match guarantees that each triplet is produced once.

Time Complexity

O(n2)
The array is first sorted in O(n log n). Then, for each of the n possible firstIndex values, we perform a linear two-pointer sweep across the subarray after it. Thus, the dominant cost is O(n × n) = O(n²).

Space Complexity

O(1) or O(n)
The sort step may use either O(1) or O(n) space depending on the algorithm implementation. Additionally, we need O(m) space to store the output list of unique triplets, where m is the number of valid solutions.

Visucodize editorial solution titled JS Solution for LeetCode's 3sum coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/3sum.