Problem
You're given a list of numbers that's already sorted in ascending order, plus a target. Find the two numbers that add up to the target and return their 1-indexed positions. Exactly one valid pair exists.
Signal
Same "find a pair that sums to target" ask as Two Sum — but this time the array arrives sorted. Sorted input is the tell: it lets two pointers converging from both ends replace the hash map entirely, no extra space needed.
Approach
Put one pointer at the front, one at the back. Compare their sum to the target. Too small means you need a bigger number, so move the left pointer right. Too big means you need a smaller number, so move the right pointer left. Exactly right means you're done. Because the array is sorted, each move provably rules out one candidate pair per step — you never have to backtrack.
Skeleton
left, right = 0, len(nums) - 1
while left < right:
total = nums[left] + nums[right]
if total == target: return [left + 1, right + 1]
if total < target: left += 1
else: right -= 1
Solution
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
left, right = 0, len(nums) - 1
while left < right:
total = nums[left] + nums[right]
if total == target:
return [left + 1, right + 1]
if total < target:
left += 1
else:
right -= 1
return []
Complexity
O(n) time — the pointers together cross the array once. O(1) space — no hash map needed, unlike the unsorted version.
Pitfalls
- Reusing the hash-map solution from Two Sum here works but throws away the free O(1)-space win the sorted input is handing you — an interviewer asking for this variant specifically wants you to notice that.
- Returning 0-indexed positions instead of the required 1-indexed ones.
- Assuming duplicates need special handling — since exactly one pair exists, moving a single pointer per step never skips the answer.