If you've solved a few coding problems on LeetCode or HackerRank, you've probably heard about the Two Pointers technique.
At first, it seems confusing.
Why use two pointers when one loop already works?
The answer is simple: efficiency.
Instead of checking every possible pair with nested loops (O(n²)), the two pointers technique often reduces the solution to O(n).
Let's understand how it works.
What Is the Two Pointers Technique?
The Two Pointers technique uses two indices (or pointers) to traverse an array or string.
These pointers move according to specific conditions until the desired result is found.
Think of it like having two people searching from different directions instead of one person checking everything.
When Should You Use Two Pointers?
Whenever you see problems involving:
- Sorted arrays
- Finding pairs
- Removing duplicates
- Palindromes
- Sliding elements from both ends
- Partitioning arrays
...there's a good chance Two Pointers can help.
Step 1: Identify the Pattern
Ask yourself these questions:
- Is the array sorted?
- Am I looking for a pair?
- Can I move one pointer based on a condition?
- Can I avoid checking every combination?
If the answer is yes, think about Two Pointers.
Step 2: Decide Where the Pointers Start
There are two common approaches.
Approach 1: Opposite Ends
left = 0
right = len(nums) - 1
Useful for:
- Two Sum II
- Container With Most Water
- Trapping Rain Water
Approach 2: Same Direction
slow = 0
fast = 0
Useful for:
- Remove Duplicates
- Move Zeroes
- Partition problems
Step 3: Define the Movement Rule
This is the most important step.
Every Two Pointers problem depends on one question:
When should each pointer move?
Example:
Suppose we need to find two numbers whose sum equals a target.
Array:
[1, 3, 5, 7, 9]
Target = 10
left = 1
right = 9
1 + 9 = 10 ✅
Another example:
1 + 7 = 8
Need a bigger sum?
Move left →
5 + 9 = 14
Too large?
Move right ←
The movement depends entirely on the comparison.
Step 4: Continue Until They Meet
The common loop looks like this:
while left < right:
...
Once both pointers cross, the search is complete.
Example Problem
Two Sum II
Problem
Given a sorted array, find two numbers whose sum equals the target.
Brute Force
for i in range(n):
for j in range(i+1, n):
Time Complexity:
O(n²)
Two Pointers
left = 0
right = len(nums)-1
while left < right:
current = nums[left] + nums[right]
if current == target:
return [left, right]
elif current < target:
left += 1
else:
right -= 1
Time Complexity:
O(n)
Space Complexity:
O(1)
Huge improvement!
Common Interview Problems
Practice these to master the pattern:
- Two Sum II
- Valid Palindrome
- Remove Duplicates from Sorted Array
- Move Zeroes
- Container With Most Water
- 3Sum
- 4Sum
- Sort Colors
- Trapping Rain Water
Common Mistakes
❌ Forgetting that the array must often be sorted.
❌ Moving the wrong pointer.
❌ Using nested loops when one pass is enough.
❌ Forgetting edge cases like duplicates.
How I Think About Two Pointers
Whenever I read a problem, I ask myself:
Can I solve this by moving one pointer instead of checking every pair?
If the answer is yes, I immediately start thinking about Two Pointers.
This mindset has helped me recognize the pattern much faster during coding interviews.
Final Thoughts
The Two Pointers technique isn't about memorizing solutions.
It's about recognizing patterns.
Once you understand where the pointers start, why they move, and when they stop, many array and string problems become much easier.
Like every DSA concept, the key is practice.
The more Two Pointers problems you solve, the faster you'll recognize the pattern in future interviews.
Thanks for reading! ❤️
If this article helped you understand the Two Pointers technique, consider leaving a ❤️ and sharing it with others who are learning Data Structures and Algorithms.
Happy Coding! 🚀
Top comments (0)