coding•DSA & Problem Solving•Arrays & Two Pointers
Two Sum II - Input Array Is Sorted
Problem Statement
Given a **1-indexed** array of integers `numbers` that is already **sorted in non-decreasing order**, find two numbers such that they add up to a specific `target` number.
Return the indices of the two numbers, `index1` and `index2`, added by one as an integer array `[index1, index2]` of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice. Your solution must use only constant extra space.
### Constraints
- `2 <= numbers.length <= 3 * 10^4`
- `-1000 <= numbers[i] <= 1000`
- `numbers` is sorted in non-decreasing order.
- `-1000 <= target <= 1000`
- Exactly one valid solution exists.
### Input Format
First line contains the target integer.
Second line contains space-separated integers representing `numbers`.
### Output Format
Print the two 1-based indices space-separated.
Sample Cases
Example 1Sample Case
Input: 9
2 7 11 15
Output: 1 2
Example 2Sample Case
Input: 6
2 3 4
Output: 1 3
Example 3Sample Case
Input: 9
2
Output:
Example 4Sample Case
Input: 9
9
Output:
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.