Two Pointers
Learn to solve problems efficiently using the two-pointer technique.
5
Problems
5
Easy
0
Medium
0
Hard
How Two Pointers Works
Two Pointers uses two index variables that move through a sorted array or sequence from different positions. The most common setup places one pointer at the start and another at the end, moving them inward based on comparisons. This eliminates the need for nested loops by making intelligent decisions about which pointer to advance. The key insight is that in a sorted array, moving the left pointer increases the sum while moving the right pointer decreases it, letting you efficiently search for target conditions.
When to Use Two Pointers
Pattern Recognition
Look for these trigger words in problem statements:
valid palindrome
two-pointers
two sum ii - input array is sorted
3sum
container with most water
trapping rain water
Common Mistakes
- Forgetting to sort the array first (two pointers requires sorted input for most problems)
- Off-by-one errors with pointer boundaries (use < vs <= carefully)
- Not handling duplicate elements when the problem asks for unique results
- Moving the wrong pointer — always reason about which direction gets you closer to the goal
When NOT to Use Two Pointers
- When the input is unsorted and sorting would lose important information (like indices)
- When you need to consider all subarrays, not just pairs
- When the data structure isn't linear (use BFS/DFS for graphs and trees)
Practice Problems
Master Two Pointers
Build pattern recognition with interactive MCQs. Understand why to use Two Pointers, not just how.
Download LeetEye Free