All lessons

Two Pointers

7 min · Python examples

Walk two indices through a sorted array to find pairs or ranges in one pass.

Techniques · Two Pointers0/5 cleared

Enemy 1 of 5

The code keeps one marker at each end of a sorted list and moves them inward, depending on whether the pair's total is too small or too big, until it finds the target. Press Run to see if a pair is found.

Why this works

Two pointers keep a left and right index. On sorted arrays they often move inward: if the sum is too small, advance left; if too big, move right.

This avoids nested loops and runs in linear time after sorting.

You
Cold-Start Frostling
Loading editor…
Target output: pair