- Algorithms
- Pointer traversals
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.
- Two Pointers: Skip Safely with an Invariant in O(1) MemoryLearn the two-pointer technique, why each pointer move never skips an answer, and how to handle crossing pointers, empty input and single items.11 min readO(n)Start here
- Sliding window: contiguous runs in a single passHow to answer questions about contiguous subarrays and substrings in O(n): grow on the right, shrink on the left, never start over.8 min readO(n)
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.