Two Pointers

Two pointers start at both ends of a sorted list and walk inward, and every step rules out every pair one number could still make.

Two Sum II: find the pair that adds up to the target

StopTwo pointers
numberstarget
  1. L20
  2. 31
  3. 42
  4. 83
  5. 114
  6. R155

1/9Start at both ends: 2, the smallest number, and 15, the largest.

  • CH1 left
  • CH2 right
Typical cost O(n) time (after any sort), O(1) spaceFull lesson: Two Sum II - Input Array Is Sorted →

When to reach for it

  • The input is sorted, or sorting it costs less than the loop it saves.
  • You want a pair or triple that hits a target.
  • The two ends alone tell you which one to move.
  • You compare from both ends: palindromes, reversals, walls that hold water.
  • The brute force checks every pair in a nested loop.

What stays true

When the sum is too big, the right number is too big even for the smallest partner left, so no pair can use it. When it's too small, the same goes for the left number. Each move drops one number for good.

Nothing ruled out was ever part of the answer

StopTwo pointers
InvariantIf a pair hits the target, it is still between left and right.✓ Holds
  1. L10
  2. 31
  3. 42
  4. 63
  5. 84
  6. R115
target10

1/11Start at both ends: 1, the smallest number, and 11, the largest.

  • CH1 left
  • CH2 right

Break it

Each input here has tripped up a version of this code. The stage runs the one that gets it right.

A loop that runs while left ≤ right pairs 3 with itself and answers 3 + 3. This one stops when the pointers meet.

Two Sum II on the inputs that trip it up

StopTwo pointers
  1. L30
  2. R51
target6

1/4Start at both ends: 3, the smallest number, and 5, the largest.

  • CH1 left
  • CH2 right
Answer[]Steps4

Variants

3Sum

16/16
StopTwo pointers
  1. -40
  2. -11
  3. -12
  4. 03
  5. 14
  6. 25
found[-1, -1, 2] [-1, 0, 1]

Fix one number, then run two pointers over the rest for its negative.

Step through it →

Container with most water

10/10
StopTwo pointers
best49
  • CH1 left
  • CH2 right
  • CH4 water

Move the shorter wall. Keeping it can only lose width.

Step through it →

Trapping rain water

14/14
StopTwo pointers
leftMax3rightMax2water6
  • CH1 left
  • CH2 right
  • CH4 water

Move the lower side. Its water level is already decided.

Step through it →

Valid palindrome

11/11
StopTwo pointers
  1. N✓0
  2. o✓1
  3. ␣2
  4. '3
  5. x✓4
  6. '5
  7. ␣6
  8. i✓7
  9. n✓8
  10. LR␣9
  11. N✓10
  12. i✓11
  13. x✓12
  14. o✓13
  15. n✓14
pairs matched5
  • CH1 left
  • CH2 right

Compare the two ends, skipping anything that isn't a letter or digit.

Step through it →

The code

The same algorithm in plain Python. The lit line is the one the current frame came from.

Two Sum II: find the pair that adds up to the target

StopTwo pointers
  1. L20
  2. 31
  3. 42
  4. 83
  5. 114
  6. R155
target15

1/9Start at both ends: 2, the smallest number, and 15, the largest.

  • CH1 left
  • CH2 right
two_sum_sorted.py
def two_sum(numbers, target):    left, right = 0, len(numbers) - 1    while left < right:        total = numbers[left] + numbers[right]        if total == target:            return [left + 1, right + 1]        if total > target:            right -= 1        else:            left += 1    return []

Drill it