← All patterns
2 / 32
Scanning & Windows

Two Pointers

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

sorted arraypair / triplettwo sumpalindromeremove duplicatesopposite ends
Practice

Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.

Ask yourself

  1. Sorted input, or can sorting help?
  2. Looking for a pair/triplet meeting a condition?
  3. Can one pass with converging pointers replace a nested loop?

Classic problem

Two Sum II — Input Array Is Sorted (LeetCode 167) — return the 1-based indices of the two numbers that add up to target. The sum’s direction tells you which pointer to move.

Input: numbers = [1, 2, 3, 4, 5, 6, 7, 8], target = 5

Output: [1, 4]numbers[0] + numbers[3] = 1 + 4 = 5, right pointer walks left 4 times before the pair is found (1-based indices).

Watch it run

Common pitfalls

Forgetting to skip duplicates in 3Sum-style problems.

Moving the wrong pointer for the condition.