This problem follows the Two Pointers Sorted pattern, commonly found in the Two Pointers category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Left and right pointers. Move left up if sum too small, right down if too large.
Because the array is sorted, moving left increases the sum and moving right decreases it — this gives you O(n) instead of the O(n²) brute force.
left = 0, right = len(nums) - 1
while left < right:
sum = nums[left] + nums[right]
if sum == target:
return [left + 1, right + 1]
else if sum < target:
left++
else:
right--Practice Two Sum II and similar Two Pointers problems with flashcards. Build pattern recognition through active recall.
Practice this problem