Recognition guide
Clues that a problem needs two pointers
Two pointers replaces a nested loop with a single pass. Two indexes move through the input by a rule, and each move rules out a region that cannot hold the answer. The hard part is not writing the loop. It is noticing, from the statement alone, that this is the loop you want.
This guide lists the clues. For the mechanics and a full worked example, see the Two Pointers pattern page.
The three shapes
Almost every two-pointer problem is one of these.
- Opposite ends. Start at the first and last positions and move inward. Pairs in a sorted row, palindromes, and walls that hold the most water all look like this.
- Same direction. A reader runs ahead and a writer marks where the next kept value goes. In-place removal and partitioning look like this.
- Two sequences. One index per input, advanced by comparison. Merging two sorted arrays looks like this.
Clues in the statement
- The input is sorted, or you are allowed to sort it. Order is what lets one comparison tell you which pointer should move. If the answer is values and not original positions, sorting first is often fine.
- "Pair", "two numbers", "triplet" that satisfy a condition. A sum, a difference, or a product target is a classic.
- "Palindrome", "reads the same", "mirror." You compare the outside characters and walk toward the middle.
- "In place", "O(1) extra space", "without a copy." A reader and a writer share one array.
- "Remove", "move", "partition", "keep the order of the rest." Same-direction pointers.
- "Merge" two ordered inputs. One pointer per input.
- "Most", "largest" area or span between two positions. The ends define the answer, so you start wide and narrow.
You rarely need all of these. Two or three together are enough to try it.
The test that matters: can one comparison discard something?
A phrase can suggest the pattern, but this question confirms it. After you compare the two current values, can you say, with certainty, that one pointer's current position can never be part of the answer?
In a sorted row, if left + right is too small, the left value can never reach the target with any remaining right value, so you move left. If you cannot make a claim like that, the pointers have nothing to base a move on and the pattern does not apply.
Counter-signals: look-alikes
- An unsorted pair with no permission to sort, and you need the positions. A too-small sum does not say which index to move. Store what you have seen in a hash map and look up the complement.
- A contiguous slice whose property you update as it grows and shrinks. The ends move in the same direction and the question is about the slice. That is a sliding window. See how to recognize a sliding window problem.
- A single boundary that halves the range. One index jumping to the middle is binary search.
- A linked list. There are no indexes to put at both ends. Two runners at different speeds find a middle, a loop, or the nth node from the end. That is Fast and Slow Pointers.
- Subsequences, where elements may be skipped. Skipping breaks the "discard a region" argument. That is usually dynamic programming.
The decision checklist
- Is the input ordered, or may you reorder it without losing what the answer needs?
- Is the answer a pair, a triple, a palindrome check, or an in-place rearrangement?
- After one comparison, can you discard one pointer's position for good?
- Is the memory limit tight enough that a set or a copy is ruled out?
- Is it a linked list? Use fast and slow pointers instead.
Two yeses from the first three is a strong sign.
Three worked examples
1. "The array is sorted. Return the two indexes whose values add up to the target."
Clues: "sorted" and "two values that add up". Too small means the left value must grow. Too big means the right value must shrink. Each comparison discards one end.
Pattern: two pointers, opposite ends.
2. "Move every zero to the end of the list. Keep the order of the other numbers. Do it in place."
Clues: "in place" and "keep the order of the rest". Nothing needs a second array. The reader scans every position. The writer marks where the next non-zero belongs.
Pattern: two pointers, same direction.
TypeScriptfunction moveZeroes(nums: number[]): void {let write = 0;for (let read = 0; read < nums.length; read++) {if (nums[read] !== 0) {[nums[write], nums[read]] = [nums[read], nums[write]];write++;}}}The idea applies in any language. The writer only moves when the reader finds a keeper. 3. "Find every unique triple of values that adds up to zero."
Clues: "triple" looks like too many pointers, but the answer is values, not positions, so you can sort. Fix one value, then the remaining two form a sorted pair problem with a target. That pair is solved with opposite-end pointers, inside one outer loop.
Pattern: sort, then two pointers inside a loop.
Common traps
- Moving both pointers after one comparison. A too-small sum says only the left value must change. Moving both can skip the one valid pair.
- Sorting away the positions. If the problem asks for original indexes, sorting renumbers them. Sort value and index pairs, or use a hash map.
- Skipping the equal case. Check for a match first, then branch on smaller or larger. Treating equal as "too small" walks past the answer.
- Duplicates in the triple problem. After you record a result, step past repeated values on both sides. Otherwise the same triple appears several times.
- Using `left <= right` for pairs. A pair needs two different positions, so the loop runs while
left < right.
Quick answers
Does it only work on arrays?
It works on any indexable sequence, including strings. On a linked list use the fast and slow variant.
Is it the same as a sliding window?
They share the idea of two indexes. In two pointers the ends usually move toward a target or toward each other. In a window both ends move forward and the slice between them is the object you reason about.
Do I always need sorted input?
For opposite-end pair problems, yes. For same-direction read and write problems, no. The order you must keep is the input order.
Practice it on Algofily
Algofily trains the recognition step before the code: each problem moves through Understand, Think, Code, and Explain. In the Code stage, Algofily AI can give progressive hints instead of the solution (sign-in required).
- Work through the Two Pointers pattern page for a full example and the problems that practice it.
- Working with linked lists? See the Fast and Slow Pointers pattern page.
While you code, Algofily AI can ask a question and open the next hint.