Algorithms / Two pointers technique

Two pointers technique

Find a pair of numbers with a given sum, then strip repeats from a list, each in a single sweep, by moving two markers through the data instead of nesting two loops.

Beginner7 steps~6 min
L / W — left pointer, or the slow writer
R — right pointer, or the fast reader
Ruled out — an item that needs no more checks
Kept — part of the finished answer
// THE SHORT VERSION

In 30 seconds

The two pointers technique walks through a list with two position markers instead of one loop inside another.

In one form the markers start at opposite ends of a sorted list and move toward each other, each check ruling out one item. In the other they travel in the same direction, a fast one reading and a slow one writing.

Either way each marker only moves forward, so the work grows in step with the list length, not with its square. It needs some order in the data to say which marker to move.

Two pointers technique, step by step

STEPS01 / 07

The pair-sum method needs a sorted row; sorting an unsorted one first costs about n log n. Both examples assume sorted input.

STEP 01 / 07 The slow way: every pair
THE MATH
for each i: for each j after i: check row[i] + row[j] 7 × 6 / 2 = 21 pairs
  1. Step 1: The slow way: every pair. The task: in a sorted row of seven numbers, find two that add up to 12. The obvious method is a loop inside a loop that tries every pair, 21 of them here. That count grows with the square of the row length, so it gets slow quickly.
  2. Step 2: Start at both ends. Put one pointer on the smallest number and one on the largest. Their sum, 14, is too big. Because the row is sorted, 13 paired with anything else would be bigger still, so 13 is ruled out for good. The right pointer steps left.
  3. Step 3: Too small? Move the left one. Now 1 + 10 = 11, which is too small. 10 is the largest number still in play, so 1 cannot reach 12 with any partner and is ruled out as well. The left pointer steps right. Whichever way a check goes, it removes one number from the search.
  4. Step 4: Close in until they match. Next, 3 + 10 = 13 is too big, so R moves. Then 3 + 8 = 11 is too small, so L moves. Then 4 + 8 = 12: found. Five sums were checked instead of 21 pairs. Had the pointers met without a match, that would prove no such pair exists.
  5. Step 5: Same direction: fast, slow. A second pattern sends both pointers the same way at different speeds. To remove repeats from a sorted row, a fast pointer R reads every item while a slow pointer W marks where the next new value goes. R has just passed a repeated 1. Now it meets a 2, which is new, so 2 is copied to W and both move on.
  6. Step 6: One pass, repeats gone. By the end R has read all seven items once and W has stopped at index 4. The front of the row now holds each value exactly once, in order: 1 2 3 5. Whatever sits from W onward is leftover and ignored. No second row was needed, and one sweep was enough.
  7. Step 7: When it applies. Two pointers work when each check tells you which pointer to move, and that move never has to be undone. Sorted order usually provides this. Each pointer only travels forward, so the total is about n steps instead of n² pairs. Without such a rule, as with pair sums in an unsorted row, the trick does not apply.
VALUES SO FAR
row1 3 4 6 8 10 13sorted
target12input
pairs21slow way
1 + 13····move R
1 + 10····move L
4 + 8····result
checks····cost
unique····result
cost····rule

Spotted a mistake on this page?