We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
Two pointers turn a nested loop into a single pass by exploiting order: move the left pointer to increase a sum, the right to decrease it, and never revisit. It shows up in pair-sum problems, palindrome checks, in-place array partitioning, and container/area questions. If the input is sorted and you are about to write two nested loops, stop and reach for this first.
A sorted input, a palindrome check, or a pair/triplet that must satisfy a condition.
24 problems. Each one runs in an in-browser editor against real test cases, with hints and a worked solution.
The patterns cheat sheet covers all of these in one page - what each one looks like in a problem statement, and the template that follows.