Two Pointer Technique —
Two indices. One elegant solution.
The two pointer technique turns O(n²) brute force into O(n) elegance. Instead of checking every pair with nested loops, two pointers walk toward each other — or in the same direction — and meet exactly when the answer is found. Watch it happen index by index on LearnBug and you'll never forget why it works.
What is the Two Pointer Technique?
Two pointer is a pattern where you maintain two index variables — usually called left and right — and move them based on a condition. It works best on sorted arrays or problems where you need to find pairs, triplets, or subarrays that satisfy a constraint. The key insight: instead of re-checking every combination, the two pointers eliminate entire sections of the array in each step.
Why visualization changes everything
Reading the code, it's easy to lose track of which pointer moves and why. On LearnBug you see both pointers highlighted on the array at every step — left creeping right, right creeping left, the target comparison updating in real time. The moment they cross, you understand immediately why the algorithm stops. No mental simulation needed.
The 3 two-pointer patterns you need to know
Every two-pointer problem is a variation of one of these three patterns. Recognise the pattern and the solution follows.
Pattern 1 — Opposite Ends
Start left at index 0 and right at the last index. Move them toward each other based on whether the current sum is too high, too low, or matches the target. Classic use case: Two Sum on a sorted array.
Pattern 2 — Same Direction (Slow & Fast)
Both pointers start at index 0 but move at different speeds. The fast pointer explores ahead while the slow pointer tracks a position. Classic use case: Remove duplicates in-place from a sorted array.
Pattern 3 — Fixed Gap
Keep a fixed distance k between the two pointers and slide them together. Classic use case: Find the Nth node from the end of a linked list, or a fixed-size sliding window on arrays.
Two Sum on a sorted array — step by step
Given [1, 3, 5, 7, 9] and target 10, find two numbers that add up to it.
Initialise pointers
left = 0 (pointing at 1), right = 4 (pointing at 9). Sum = 1 + 9 = 10. That's the target — done in one step!
Sum too large → move right pointer left
If sum was 12 (too big), we'd move right--. A bigger right value made the sum too large, so shrink it.
Sum too small → move left pointer right
If sum was 4 (too small), we'd move left++. A smaller left value made the sum too small, so grow it.
Pointers cross → no solution exists
When left >= right, we've checked all valid pairs. Exit the loop. This is why the algorithm is O(n) — each pointer moves at most n times total.
The pattern in code
def two_sum_sorted(nums, target):
left = 0
right = len(nums) - 1
while left < right:
current_sum = nums[left] + nums[right]
if current_sum == target:
return [left, right] # found it
elif current_sum < target:
left += 1 # need bigger sum
else:
right -= 1 # need smaller sum
return [] # no pair foundTime & Space Complexity
When should you reach for two pointers?
The array is sorted
Sorted order means moving a pointer in one direction guarantees the sum goes up or down — that's what makes two pointer work.
You're looking for a pair or triplet
Problems asking for two numbers that sum to target, or three numbers summing to zero, are classic two-pointer setups.
You need to compare elements from both ends
Palindrome check, reversing an array in-place, container with most water — all move from both ends inward.
You need to partition in-place
Removing duplicates, moving zeroes to end, Dutch National Flag — all use a slow pointer to track the write position.
Frequently asked questions
Does the array have to be sorted for two pointer to work?
For the opposite-ends pattern (Two Sum), yes — sorting is essential because it gives you a predictable direction to move each pointer. For the same-direction pattern (remove duplicates, move zeroes), the array doesn't need to be sorted. Always check which variant of the pattern applies before deciding.
How is two pointer different from sliding window?
Both use two indices, but the intent differs. Two pointer typically looks for a specific pair or partition condition — pointers move independently based on comparisons. Sliding window maintains a contiguous subarray of variable or fixed size and tracks an aggregate (sum, count, max) over that window. Many problems can be framed either way, but sliding window is the better label when you're tracking a running total over a subarray.
What is the time complexity of two pointer?
O(n). Each pointer starts at one end and moves toward the other — they never go backward. So the total number of moves across both pointers is at most n, giving linear time. This is the key advantage over brute-force O(n²) nested loops.
Can two pointer work on strings?
Yes. Palindrome check is the classic example — place one pointer at the start and one at the end, compare characters, and move inward. Valid palindrome, reverse string, and longest palindromic substring all use this idea.
What are common interview problems that use two pointer?
Two Sum II (sorted), Three Sum, Container With Most Water, Remove Duplicates from Sorted Array, Move Zeroes, Valid Palindrome, Trapping Rain Water, and Merge Sorted Arrays. If you see a problem on a sorted array asking for a pair — reach for two pointer first.
Watch two pointers move on your own code
Paste your array problem into LearnBug and see left and right highlight at every step.