Given a sorted array, find two numbers that add up to target.
python
def two_sum_sorted(nums: list[int], target: int) -> tuple[int, int] | None: left, right = 0, len(nums) - 1 while left < right: total = nums[left] + nums[right] if total == target: return left, right if total < target: left += 1 else: right -= 1 return None
Complexity
Approach
Time
Space
Brute force
O(n²)
O(1)
Two pointers
O(n)
O(1)
Hash map (unsorted)
O(n)
O(n)
In an interview, say the sorted assumption out loud. If the array isn't sorted, ask whether you may sort — that's often the difference between O(n) and O(n log n).