This content originally appeared on DEV Community and was authored by Martin Lynch
TL;DR: Two pointers is a powerful pattern for solving problems in sorted arrays. It’s faster than brute force and unlocks elegant solutions for many common interview questions.
If you’ve ever seen a Leetcode solution that says “use two pointers” and thought:
“Yeah sure… but why does that work?”
You’re not alone.
Two pointer problems look simple — but they feel like magic when you’re new. So let’s break it down the way I wish someone had explained it to me: like we’re pair programming side by side.
What Is the Two Pointers Technique?
Two pointers is a pattern where you use two indices (or “pointers”) to traverse an array — either from both ends or from the same direction — to solve a problem more efficiently than brute force.
It’s mostly used when:
- The array is sorted
- You’re looking for relationships between elements (like sums, duplicates, or partitions)
Problem: Find Two Numbers That Add Up to a Target
Let’s say you’re given this problem:
Given a sorted array of integers, return the indices of the two numbers that add up to a specific target.
const nums = [1, 2, 4, 7, 11, 15];
const target = 15;
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
This works.
But it’s O(n²) time complexity — bad news if your array is huge.
The Two Pointers Version
Since the array is sorted, we can use a more efficient strategy.
let left = 0;
let right = nums.length - 1; // Get the last element in the array
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) return [left, right];
if (sum < target) left++; // move toward a bigger sum
else right--; // move toward a smaller sum
}
This is O(n) time.
Much cleaner.
Way faster.
Visual Walkthrough
Let’s visualize how the pointers move:
Index: 0 1 2 3 4 5
Array: [1, 2, 4, 7, 11, 15]
↑ ↑
left right
1 + 15 = 16 > target → move right inward
Index: 0 1 2 3 4 5
Array: [1, 2, 4, 7, 11, 15]
↑ ↑
left right
1 + 11 = 12 < target → move left inward
Index: 0 1 2 3 4 5
Array: [1, 2, 4, 7, 11, 15]
↑ ↑
left right
2 + 11 = 13 < target → move left again
Index: 0 1 2 3 4 5
Array: [1, 2, 4, 7, 11, 15]
↑ ↑
left right
4 + 11 = 15 ✅ match!
Why Two Pointers Actually Work
Think of it like this:
- If the current sum is too small, move the left pointer right (bigger number).
- If the current sum is too large, move the right pointer left (smaller number).
- Since the array is sorted, each movement helps you zero in on the target sum.
You’re not checking every pair — just the ones that could logically work.
Other Common Use Cases for Two Pointers
- Reversing an array or string
function reverseString(s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
[s[left], s[right]] = [s[right], s[left]];
left++;
right--;
}
return s;
}
- Removing duplicates from a sorted array (in-place)
function removeDuplicates(nums) {
if (nums.length === 0) return 0;
let i = 0;
for (let j = 1; j < nums.length; j++) {
if (nums[i] !== nums[j]) {
i++;
nums[i] = nums[j];
}
}
return i + 1; // length of the unique portion
}
- Valid Palindrome Check
function isPalindrome(s) {
s = s.replace(/[^a-z0-9]/gi, '').toLowerCase();
let left = 0;
let right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
Tips for Recognizing Two Pointer Problems
Ask yourself:
- Is the array sorted?
- Can I traverse from both ends?
- Is there a condition involving a sum, a match, or a reversal?
- Would brute force be O(n²) but I only need one pair/position?
If yes to any of those — try two pointers.
Final Thought
Two pointers seem like a clever trick until you understand the logic behind them. Once it clicks, they become one of your go-to tools for solving problems faster and cleaner.
This content originally appeared on DEV Community and was authored by Martin Lynch