Practice Two Sum II - Input Array Is Sorted
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
numbers is sorted in nondecreasing order and has exactly one pair of distinct positions whose values sum to target. Return those positions in increasing order, using 1-based indexing. A position cannot be used twice, and the solution must use constant extra space. Approach: Put one pointer at each end. If their sum is too small, advance the left pointer; if it is too large, move the right pointer left. Return the matching positions as 1-based indices. An implementation with zero-based array offsets adds one exactly once when producing the result. This takes O(n) time and O(1) extra space.
Source: Two Sum II - Input Array Is Sorted on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
numbers = [-3,1,3,8], target = 5
[1,4]
The values -3 and 8 add to 5 and occupy the first and fourth positions.
Constraints
How to solve Two Sum II - Input Array Is Sorted
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
Because the array is sorted, the sum of the two outermost candidates can be adjusted monotonically: moving the left pointer right can only increase the sum and moving the right pointer left can only decrease it, so one pointer move per step never discards the unique answer.
Why brute force is not enough
Checking every pair is O(n^2), about 4.5 * 10^8 pairs for n up to 3 * 10^4. A hash map gives O(n) time but O(n) extra memory, which the constant-space requirement rules out.
Approach
- Place a left pointer at the first index and a right pointer at the last.
- While left is before right, compute the sum of the two values.
- If the sum equals the target, return the two indices plus one each for 1-based output.
- If the sum is too small, the left value cannot pair with anything to the left of right either, since those values are no larger, so advance left. If the sum is too large, retreat right by the symmetric argument.
- The loop is guaranteed to find the pair because exactly one solution exists and each move provably discards only impossible pairs.
Walkthrough of Example 1
numbers = [-3,1,3,8], target = 5 → [1,4]
- Left is at index 0 holding -3; right is at index 3 holding 8.
- Their sum is -3 + 8 = 5, which equals the target on the first comparison.
- Convert zero-based indices 0 and 3 to 1-based positions 1 and 4 and return [1,4].
Time and space
Time O(n) · Space O(1). n is the array length; the pointers move toward each other at most n times in total, and only two indices are stored.
Interview pitfalls
- Returning zero-based indices or adding one twice; the expected answer uses 1-based positions.
- Allowing left to equal right, which would reuse one element; the loop must stop when they meet.
- Falling back to a hash map out of habit when the sorted order and the space constraint are the whole point of the problem.
- Being unable to justify why moving a pointer never skips the answer; interviewers often ask for the exchange argument.
- Negative values and duplicates do not break the method, but candidates sometimes add unnecessary special cases for them.
Follow-up questions interviewers ask
- How would you find all pairs when multiple solutions exist, including handling duplicates without repeating pairs?
- Extend to three numbers summing to a target (3Sum) using the same two-pointer core.
- If the array were not sorted, would you sort it first or use a hash map, and when does each win?
Related LeetCode Problems
Practice more problems similar to Two Sum II - Input Array Is Sorted.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.