<>Topic

Two Pointers Problems

Two-pointer problems — palindromes, sorted-array pair sums, and in-place partitioning in O(n) time and O(1) space.

Easy · 1

Medium · 2

The two-pointer pattern is one of the highest-leverage techniques you can learn — it converts O(n²) brute-force scans into O(n) linear sweeps with O(1) extra space. The pattern shows up whenever the input is sorted (or can be sorted), or when you can grow and shrink a window from both ends.

There are three canonical forms. Converging pointers start at opposite ends and walk inward — used for palindrome checks, container-with-most-water, and 2-sum on sorted arrays. Same-direction pointers advance at different speeds — used for in-place duplicate removal and Floyd's cycle detection. Anchored pointers fix one index and sweep the other — used inside three-sum and four-sum where you reduce a higher-arity search to a 2-pointer scan.

The Ratta two-pointers track covers Valid Palindrome, Three Sum, Container With Most Water, and trapping-rain-water style problems. Each problem includes the geometric intuition for why the pointer move is safe to make, the invariant that holds at every step, and complete solutions in C++, Java, Python, and Go.

Browse other topics