Algorithms Playbook
PatternTime O(n) · Space O(1)8 min read

Two pointers: turn an O(n²) search into one pass

Walk two indices through the data instead of checking every pair. Why it's correct, the two main shapes (from both ends, and same direction) and how to spot it in an interview.

In short

Put two indices in the array and move them by a rule so that every move rules out many candidates at once. Checking all pairs is O(n²); two pointers does it in one pass, O(n), with no extra memory.

The idea: stop checking every pair

Find two numbers in a sorted array that add up to a target. The brute force tries every pair: with 10,000 numbers that's about 50 million sums.

Two pointers starts at both ends. If the sum is too small, the only way to grow it is a bigger left number, so left moves right. If it's too big, right moves left. Each step throws away a whole row of pairs, so you're done in at most n steps.

Watch it run

Step through it and read why each pointer moves. Try a target that doesn't exist and watch the pointers meet.

Two pointers: a pair that adds up to the target
Example
Speed
0113243648510613715818921leftrighttarget = 19
1def pair_with_sum(nums, target):
2 left, right = 0, len(nums) - 1
3 while left < right:
4 total = nums[left] + nums[right]
5 if total == target:
6 return [left, right]
7 if total < target:
8 left += 1
9 else:
10 right -= 1
11 return []
One pointer at each end of the sorted array. Together they'll walk towards the middle.

Variables

left0
right9
total—
target19
Step 1/7· Sums checked: 0
Because the array is sorted, every comparison rules out a whole row of pairs at once. One pass instead of checking all n² pairs.

Why it never misses the answer

When nums[left] + nums[right] is too small, pairing nums[left] with anything left of right is even smaller (the array is sorted). So no pair that uses the current left can work: moving it loses nothing. The same logic applies to right when the sum is too big.

The two shapes of the pattern

From both ends (opposite direction)

  • Pair or triplet with a target sum (3Sum = loop + two pointers)
  • Valid palindrome
  • Container with most water
  • Reverse an array in place

Same direction (slow and fast)

  • Remove duplicates from a sorted array in place
  • Move all zeros to the end
  • Merge two sorted arrays
  • Fast/slow pointers to find a cycle in a linked list

Sliding window is a close cousin: two pointers in the same direction that define a range, used for subarrays and substrings. It gets its own lesson.

How to recognise it

  • The input is a sorted array or a string, and you need pairs or triplets.
  • The brute force is two nested loops.
  • You're asked to do it in place or with O(1) extra space.
  • You compare elements from both ends (palindromes, reversing).

Key takeaways

  • 01Two pointers replaces nested loops with one pass: O(n) time, O(1) space.
  • 02Each move must rule out every candidate that uses the element you move.
  • 03Two shapes: from both ends (pairs, palindromes) and same direction (in-place edits, merging).
  • 04Sorted input + pairs + O(1) space is the strongest signal.

Check your understanding

1 of 3

In a sorted array, left + right is smaller than the target. Which pointer moves?

Related topics