Two Pointer Technique Arrays
Fast recall
Quick revision
Core answer
left right, if too big move right left. It turns many O(n^2) pair problems into O(n) with O(1) space.Whether you know the precondition (often sorted) and can explain why moving a pointer is provably safe
Using two pointers on unsorted data for sum problems, or not justifying which pointer to move
Explain the monotonic reasoning: in a sorted array, moving left/right strictly increases/decreases the sum
Interview-ready guide
Interview answer
The two-pointer technique uses two indices into an array that I move based on some condition, and the payoff is replacing a nested loop, which would be O of n squared, with a single linear pass.
The most common form works on a sorted array, with one pointer at the start and one at the end. Say I'm looking for two numbers that add up to a target. I add the values at the two pointers.
If the sum is too small, the only way to increase it is to move the left pointer to the right, toward larger values, because the array is sorted. If the sum is too big, I move the right pointer left, toward smaller values. If it matches the target, I've found my pair.
The reason this is correct, and what I'd emphasize, is the monotonic reasoning. Because the array is sorted, when the sum is too small I know that pairing the current right value with anything smaller than the current left value is hopeless, so I can safely discard that left pointer position and never miss a valid pair. Each move eliminates a whole batch of impossible combinations.
The big precondition is that the array has to be sorted for this sum version; on unsorted data I'd either sort first or use a hash map instead. There's also a same-direction variant, a slow and a fast pointer moving the same way, which I use for in-place tasks like removing duplicates or for cycle detection in a linked list.
Either way, two pointers give me O of n time and constant extra space.
Two Pointer Technique Arrays
Direct Answer
The two-pointer technique uses two indices that move toward each other (or in the same direction) to avoid a nested loop. On a sorted array, put one pointer at each end: if the pair sum is too small move left right, if too big move right left. It turns many O(n^2) pair problems into O(n) with O(1) space.
Deep Dive & Architecture
The opposite-end two-pointer pattern is useful when the array is sorted and a decision can safely remove one side of the remaining range. One pointer begins at the smallest value and the other at the largest.
Worked example: [1, 2, 4, 6, 10], target 8
The first sum is 1 + 10 = 11. It is too large, so move the right pointer left; pairing 10 with any value to the right of 1 would only be larger. Now 1 + 6 = 7, which is too small, so move the left pointer right; pairing 1 with any remaining value cannot reach 8. The next sum is 2 + 6 = 8, so the pair is found.
That elimination argument is the invariant. Without sorted order, changing a pointer based on the sum is not safe. For an unsorted pair-sum problem, a hash set is normally the O(n)-time choice, or the array can be sorted if original indexes are not required.
Two pointers also appear in same-direction form. A slow pointer can mark the next valid write position while a fast pointer scans, as in removing duplicates from a sorted array. The pattern is O(n) because neither pointer moves backward.
Code Example
int[] pairSum(int[] nums, int target) { // nums must be sorted
int left = 0, right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) return new int[]{left, right};
else if (sum < target) left++; // grow the sum
else right--; // shrink the sum
}
return new int[]{-1, -1};
}
// nums = {1, 3, 4, 6, 8}, target = 10
// 1+8=9 <10 -> left++; 3+8=11 >10 -> right--; 3+6=9 <10 -> left++; 4+6=10 -> [2,3]
Common Mistakes & Pitfalls
- Unsorted input: the opposite-end sum technique needs a sorted array; sort first or use a HashMap.
- Wrong pointer move: moving the wrong pointer can skip the answer - tie the move to the comparison.
- Off-by-one / overlap: use
left < rightso the two pointers never cross or point at the same element. - Forgetting duplicates: for problems like 3Sum, skip equal values to avoid duplicate results.
Check your understanding
Answer these without looking back at the article.
- What is the time complexity of common array operations in Java?
- How do you reverse a string in Java without using built-in reverse methods?
- How do you find duplicates in an array in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.