Skip to content
AI360Xpert
Beta
Difficulty: MediumTwo Pointers

3Sum

Problem in Plain English

Given an integer array nums, return all unique triplets of values that add up to zero, where the three values come from three different positions. No duplicate triplet may appear in the answer.

Problem Statement

Given an integer array nums, return all unique triplets [nums[i], nums[j], nums[k]] with three different indices i, j and k such that nums[i] + nums[j] + nums[k] == 0.

The answer must not contain duplicate triplets: two triplets with the same three values in any order count as one, and the triplets may be returned in any order.

Constraints

  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5

Examples

Example 1

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

The index triplets (0, 1, 2) and (0, 3, 4) give (-1) + 0 + 1 = 0 and (-1) + 2 + (-1) = 0, which are the distinct triplets [-1, 0, 1] and [-1, -1, 2].

Example 2

Input
nums = [0,1,1]
Output
[]

The only triplet available is [0, 1, 1], which sums to 2, so no triplet qualifies.

Example 3

Input
nums = [0,0,0]
Output
[[0,0,0]]

The single available triplet [0, 0, 0] sums to 0, so it is the whole answer.

Example 4

Input
nums = [-2, 0, 0, 2, 2]
Output
[[-2, 0, 2]]

Several index choices use the repeated zero and two, but they all give the same value triplet. Return it only once.

Example 5

Input
nums = [-100000, 0, 100000]
Output
[[-100000, 0, 100000]]

The smallest and largest allowed values cancel with zero. Values may be far apart after sorting, so the pair scan starts at both ends.

Intuition

Three different positions must contribute to a sum of zero, but the answer identifies value combinations rather than positions. Example 1 finds [-1, 0, 1] at more than one index combination, yet it appears only once in the result. Fix one value and search for two others whose sum is its negative. Sorting lets a small pair sum be increased by moving the left pointer and a large pair sum be decreased by moving the right pointer.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
If the array were sorted, fixing one value would leave a familiar two-number search for the remaining sum.
Hint 2
With two pointers on a sorted range, when is it safe to move the left pointer, and when the right one?
Hint 3
Equal neighbours produce equal triplets, so each pointer must jump past values it has already used.

Edge Cases

  • Exactly three elements either produce one triplet or none. Arrays shorter than three are outside this problem's constraints.
  • All zeros such as [0, 0, 0, 0] produce just [[0, 0, 0]]; three distinct positions can still hold equal values.
  • Heavy duplicates such as [-2, 0, 0, 2, 2] produce only [[-2, 0, 2]]. Skipping repeated anchors alone is insufficient.
  • All-positive or all-negative arrays have no answer. No arbitrary position may be reused to cancel a value.
  • Under the stated bounds, a three-value sum lies within [-300000, 300000], safely represented by Java int and JavaScript Number. Reassess overflow if value bounds change.
  • The two-pointer implementation sorts nums in place. If preserving the original order is required by a caller, sort a copy and account for its O(n) allocation.

Common Mistakes and Interview Tips

  • Returning index combinations or repeated permutations answers a different question: return unique value triplets.
  • Using left equal to i, or accepting left equal to right, reuses a position. Start left at i + 1 and require left < right.
  • Moving pointers by the sum before sorting has no monotonic-order guarantee. In JavaScript, supply a numeric sort comparator rather than the default lexical sort.
  • Skipping duplicates only for the anchor can emit the same triplet multiple times. After a match, move both endpoints and skip both repeated values.
  • Interview tip: explain each safe discard with sorted order and distinguish scan variables, sort workspace, and result storage. The quadratic scan improves the cubic baseline; no claim of constant total storage applies when the output itself can be quadratic.

Key Takeaway

Sorting plus two pointers turns a K-sum search into a fixed-prefix loop around a linear two-sum scan, with duplicate-skipping keeping the output unique.