Skip to content
Algorithms for Beginners

Problems / Two pointers

Three to zero

medium

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. Press Submit. It runs hidden tests too. Pass them all, and it's solved.

Given a list of whole numbers nums, find every set of three numbers from different positions that adds up to exactly 0.

Return a list of these triples. Each triple should appear only once: [-1, 0, 1] and [0, 1, -1] count as the same triple, so return just one of them.

The triples, and the numbers inside each triple, can be in any order.

Examples

  1. Example 1

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

    -2 + -2 + 4, -2 + -1 + 3 and -1 + 0 + 1 all make 0. The two -2s are at different positions, so both can be used.

  2. Example 2

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

    Many ways to pick three zeros, but they all make the same triple.

  3. Example 3

    Input
    nums = [1, 2, 3]
    Output
    []

    No three numbers add up to 0.

Rules

  • nums has between 0 and 3,000 numbers, from -100,000 to 100,000.
  • The same value can appear more than once in nums, and can then be used more than once in a triple.
Words on this page, in plain English (2)Open if any word is new to you
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Index (position)
Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function threeToZero(nums) {
  // Return a list of triples, like [[-1, 0, 1]].
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in two pointers

  • Learn the two pointers pattern

    Walk through a list from both ends (or at two speeds) at once.

  • Mirror phrase

    Easy. Check whether a phrase reads the same backwards, ignoring spaces, punctuation and capitals.

  • Flip in place

    Easy. Reverse a list by swapping items inside it, without building a new list.

  • Pair in a sorted list

    Easy. Find two prices that add up to a budget, when the prices are already sorted.

  • Squash repeats

    Easy. Remove repeated values from a sorted list in place, keeping one of each.

  • Biggest tank

    Medium. Choose two walls that, with the ground between them, hold the most water.