Skip to content
Decoder

3Sum

Medium

Problem

Given a list of integers nums, return every unique triplet [a, b, c] of values taken from three different positions where a + b + c == 0. Two triplets with the same values count as the same triplet, so do not repeat them. The order of the triplets, and of the values inside each one, does not matter.

Example 1

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

Example 2

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

Example 3

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.