Master the efficient Two Pointer technique to solve the sorted Two Sum problem in linear time and constant space.
You’ve probably seen the classic Two Sum problem before—it’s practically a rite of passage on LeetCode. Most people solve it with a Hash Map, getting that sweet time complexity (though at the cost of space).
But here’s where Two Sum II gets interesting. There’s one critical difference: the array is already sorted. And that changes everything. We can toss the Hash Map aside completely and solve this with constant space while still keeping linear time. It’s the perfect introduction to the Two Pointer technique—a pattern you’ll use again and again.
You’re given a 1-indexed array of integers called numbers that’s sorted in non-decreasing order. Your job? Find two numbers that add up to a specific target.
Here’s what you need to know:
[index1, index2] where both are 1-indexed.Think of it like this: You’ve got a $50 gift card and you’re standing in front of a shelf where products are arranged by price—cheapest on the left, most expensive on the right. You need to buy exactly two items that total $50.
So you grab the cheapest item (left side) and the most expensive item (right side):
You keep moving inward from both ends until you hit exactly $50. That’s the Two Pointer technique in a nutshell.
Before we jump into code, try finding the target sum yourself with this interactive tool. Pay attention to how the sorted order basically tells you which way to move next.
No actions yet. Start the game!
Sure, you could use Brute Force (—yikes) or Binary Search (—better, but not optimal). But when you’ve got a sorted array, the Two Pointer technique is the way to go.
Here’s the game plan:
left at index 0 and right at the last index.[left + 1, right + 1] (remember the 1-indexing).left to the right.right to the left.The magic here: You never have to backtrack. If numbers[left] + numbers[right] is too big, then numbers[right] is too large to pair with anything from left onward. You can eliminate it permanently and move on.
Watch how the pointers close in on the answer:
Time Complexity: O(n) - Single pass through the array
Space Complexity: O(1) - Only two pointers used
Let’s see this in action. Here’s the code in Java, C++, and Python. Notice how we adjust for 1-based indexing in the return statement (just add 1 to our 0-based indices).
class Solution {
public int[] twoSum(int[] numbers, int target) {
// Start from both ends
int left = 0;
int right = numbers.length - 1;
while (left < right) {
int sum = numbers[left] + numbers[right];
if (sum == target) {
// Found it! Convert to 1-based indices
return new int[]{left + 1, right + 1};
} else if (sum < target) {
// Need a bigger sum
left++;
} else {
// Need a smaller sum
right--;
}
}
// We shouldn't get here (problem guarantees a solution exists)
return new int[]{-1, -1};
}
} Why is Two Pointers better than the alternatives?
Let’s break it down:
target - element). This gives you . Not bad, but we can do better.Empty
Time: O(n) | Space: O(n)
Works on unsorted arrays
Not started
Time: O(n) | Space: O(1)
Requires sorted array
Time Complexity : O(N)
We traverse the array once, max. The left pointer only moves right, right only moves left. They meet somewhere in the middle, doing constant work at each step.
Space Complexity : O(1)
Just two integer variables for our pointers. That’s it. Unlike the original Two Sum, we don’t need a Hash Map because the sorted array is our lookup structure.
Watch out for these gotchas:
left right when the sum’s too small. Move right left when it’s too big. Mix this up and you’ll be debugging for a while.numbers[left] + numbers[right] could overflow in C++ or Java. But the constraints () keep us safe here.Two Sum II is a textbook example of how constraints (in this case, a sorted array) can completely transform your approach. The Two Pointer technique gives us the best possible performance—both time and space.
💡 Key takeaway: Whenever you see “sorted array” in a problem, your brain should immediately think “Two Pointers or Binary Search?”
This pattern shows up everywhere—3Sum, Container With Most Water, Trapping Rain Water. Get comfortable with it here, and those tougher problems become a lot more manageable.