Skip to content

Pointer traversals

Ways to walk an array or a string once by moving two indexes carefully. They solve many problems about pairs, subarrays and substrings.

2 guides19 min in totalAlgorithms

The guides, in order

Read them top to bottom: each one builds on the ones before. The ones you've read carry a check.

About this group

What you'll find
Two pointers, from both ends or at two speeds, and the sliding window for contiguous ranges, with the invariant that justifies each move.
It's for you if…
you're asked about subarrays, substrings or pairs and want to go from O(n²) to O(n).
How each guide works
Each guide starts with the intuition and the signs that tell you to use the technique, then the steps and its time and memory cost. It includes a step-by-step traced example, code in TypeScript, Python and PHP, variants, common mistakes and frequently asked questions.