← All posts
November 21, 2026 · 3 min read

3Sum (LeetCode 15) in Python: Sort and Two Pointers

3Sum (LeetCode 15): sort, fix one number, walk two pointers over the rest, and skip repeats. Every zero-sum triple once, in O(n²).

3Sum (LeetCode 15) gives you a list of integers and asks for every triple of them that adds up to exactly zero, with each triple listed once. Our example is LeetCode's own: [-1, 0, 1, 2, -1, -4].

Trying every triple works, but n numbers have about n³/6 triples, and you still have to throw away the repeats. Sorting first lets two pointers do the searching and makes the repeats easy to skip.

How does a seesaw help?

Put three numbers on a seesaw. If their sum is below zero it tips left, if it is above zero it tips right, and at exactly zero it balances. The search is a matter of reading which way it tips and changing the right number.

The six numbers -1, 0, 1, 2, -1, -4 as paper tiles under a seesaw that tips left, the side marked < 0

Why sort, and what do the pointers do?

Sort the list: [-4, -1, -1, 0, 1, 2]. Then fix one number at a time, at index k, and look for two more to its right that cancel it. Put i just after k and j at the end, and while i is left of j:

  1. add the three: s = a[k] + a[i] + a[j];
  2. if s is too low, move i right, to a bigger number;
  3. if s is too high, move j left, to a smaller number;
  4. if s is zero, keep the triple and move i on.

Because the list is sorted, each move changes the sum in a known direction, so no pair the loop skips over could have balanced. That is the same two-pointer idea as Two Sum II, run once for every fixed number.

Sorted tiles -4, -1, -1, 0, 1, 2 with k on -4, i on the first -1 and j on 2; the readout says -4 + -1 + 2 = -3 and the seesaw tips left

How does the walk go on [-4, -1, -1, 0, 1, 2]?

k i j Sum Seesaw Then
-4 -1 2 -3 too low i moves
-4 -1 2 -3 too low i moves
-4 0 2 -2 too low i moves
-4 1 2 -1 too low i meets j
-1 -1 2 0 balanced keep, i moves
-1 0 2 1 too high j moves
-1 0 1 0 balanced keep, i meets j
-1 (again) skipped
0 1 2 3 too high j meets i

With -4 fixed, every sum is too low: even the two biggest numbers, 1 and 2, only reach -1.

With the first -1 fixed, -1, -1 and 2 balance, then -1, 0 and 2 is one too high, so j steps left, and -1, 0 and 1 balance.

k on the first -1, i on the second -1, j on 2: the three tiles turn teal, the seesaw is level, the readout says -1 + -1 + 2 = 0 and found 1, with out.append highlighted

k on -1, i on 0, j on 1: the tiles turn teal, the readout says -1 + 0 + 1 = 0 and found 2, with out.append highlighted in the Python

How are repeated triples skipped?

The second -1 would fix the same value again and find -1, 0, 1 a second time. So when a[k] equals the number before it, the loop skips it with continue. The same can happen with the middle number: after a triple is kept, i steps past any copies of the value it just used, so [-2, 0, 0, 2, 2] gives [-2, 0, 2] once, not twice.

The second -1 tile is greyed out and the line if k > 0 and x == a[k - 1]: continue is highlighted

What does the program look like?

This is the program from the video, line for line:

def three_sum(a):
    a.sort()
    out = []
    for k, x in enumerate(a):
        if k > 0 and x == a[k - 1]: continue
        i, j = k + 1, len(a) - 1
        while i < j:
            s = x + a[i] + a[j]
            if s < 0:
                i += 1
            elif s > 0:
                j -= 1
            else:
                out.append([x, a[i], a[j]])
                i += 1
                while i < j and a[i] == a[i - 1]:
                    i += 1
    return out
print(three_sum([-1, 0, 1, 2, -1, -4]))

Output:

[[-1, -1, 2], [-1, 0, 1]]

We also checked it against a brute force that tries every triple and removes repeats, on 3,000 random lists: the two always agree.

Why is it O(n²)?

Sorting takes O(n log n). Then each fixed number starts one two-pointer walk, and each walk moves i and j toward each other at most n steps in total. That is n walks of at most n steps: O(n²) time, which beats the O(n³) of trying every triple. Apart from the answer, the extra memory is a few variables (Python's sort itself can use up to O(n)).

The full program on screen under the readout sort + two pointers: O(n²)

Try it

Run the same function on four zeros: [0, 0, 0, 0]. Four zeros can be picked three at a time in four ways. How many triples does three_sum return, and which lines of the code decide that?