Two Pointers
When an array is sorted and the question is about pairs, triplets, or a palindrome, two pointers usually beats a hash map — same or better time complexity, and it does it in O(1) space.
How to recognize it
Two pointers shows up whenever an array or string is sorted (or can cheaply be treated as sorted) and the question involves comparing elements from two positions — a pair that sums to a target, a palindrome check, removing duplicates in place, or merging two sorted structures. The recognition cue is subtler than arrays-and-hashing: the word "sorted" may not appear explicitly, but a constraint like "the array is in non-decreasing order" is the same signal.
There are two distinct shapes. Converging pointers start at opposite ends and move toward each other — used for pair sums and palindrome checks. Fast/slow pointers move in the same direction at different rates — used for cycle detection and in-place deduplication.
How the pattern works
The classic converging example: given a sorted array, find two numbers that sum to a target. Start one pointer at the beginning and one at the end. If the sum is too large, move the right pointer inward (making the sum smaller); if too small, move the left pointer inward. Because the array is sorted, this always converges without missing the answer.
Python
def two_sum_sorted(nums, target):
left, right = 0, len(nums) - 1
while left < right:
total = nums[left] + nums[right]
if total == target:
return [left, right]
elif total < target:
left += 1
else:
right -= 1
return []JavaScript
function twoSumSorted(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const total = nums[left] + nums[right];
if (total === target) return [left, right];
if (total < target) left++;
else right--;
}
return [];
}Say why moving a pointer is safe before you do it: "the sum is too small, and since the array is sorted, moving the right pointer left can only make it smaller — so I have to move the left pointer right instead." That justification is the part that distinguishes understanding the pattern from having memorized it.
Common mistakes
- Reaching for two pointers on an unsorted array without first checking whether sorting it is even valid for the problem (it is not, if original indices matter and are part of the answer)
- Off-by-one errors in the loop condition — using
left <= rightwhen the problem requires two distinct elements, which should beleft < right - Forgetting to skip duplicate values when the problem asks for unique pairs or triplets (common on the 3Sum-style extension of this pattern)
- Defaulting to two pointers on every array problem out of habit — if the question is about a contiguous range rather than a pair, it is Sliding Window, not this
Complexity
Two pointers on an already-sorted input is O(n) time, O(1) space — strictly better on space than the equivalent hash-map approach, which is the whole reason to prefer it once the array is sorted. If you have to sort first, the total is O(n log n), still often preferable to a hash-map alternative when the interviewer follow-up is "can you do it without extra memory." See Time & Space Complexity for how to frame that trade-off out loud.
Frequently asked questions
- How do I know a problem needs two pointers instead of a hash map?
- The strongest signal is a sorted array or string plus a question about pairs, triplets, or a palindrome property. If sorting is already given (or cheap to do) and you are looking for elements that satisfy a comparison, two pointers usually beats a hash map on space, trading O(n) extra memory for O(1).
- Do the two pointers always start at opposite ends?
- No — that is one variant (converging pointers, common in "pair sums to target" or palindrome checks). The other variant is two pointers moving in the same direction at different speeds, which shows up in cycle detection and removing duplicates from a sorted array in place.
- What if the array is not sorted?
- Either sort it first if order does not matter for the answer (costs O(n log n) but often still beats the alternative), or the problem is probably not a two-pointers problem — reconsider whether it is actually arrays-and-hashing or sliding window.
Related
Practice patterns weighted to your level
Free account. DSA emphasis and difficulty scale with your target level.