Two Sum II – Input Array Is Sorted
Problem Statement
Section titled “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.
Examples
Section titled “Examples”| Input | Target | Output |
|---|---|---|
[2, 7, 11, 15] | 9 | [1, 2] |
[2, 3, 4] | 6 | [1, 3] |
[-1, 0] | -1 | [1, 2] |
Constraints
Section titled “Constraints”2 <= numbers.length <= 3 × 10^4-1000 <= numbers[i] <= 1000numbersis sorted in non-decreasing order-1000 <= target <= 1000- Exactly one valid answer exists
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Notes |
|---|---|---|---|
| Two Pointers | O(n) | O(1) | Optimal solution, requires sorted array |
* n = length of the input array
Approach: Two Pointers (Recommended)
Section titled “Approach: Two Pointers (Recommended)”Place one pointer at the start of the array and one at the end. Compute the sum of the two pointed-at values. If the sum equals the target, return the 1-indexed positions. If the sum is less than the target, advance the left pointer to increase the sum. If the sum is greater, retreat the right pointer to decrease it.
This works because the array is sorted: moving left right can only increase the sum; moving right left can only decrease it. Every step eliminates at least one element from consideration.
Step-by-step Example
Section titled “Step-by-step Example”Input: numbers = [2, 7, 11, 15], target = 9
| Step | left | right | sum | action |
|---|---|---|---|---|
| 1 | 0 (2) | 3 (15) | 17 | > 9 → right-- |
| 2 | 0 (2) | 2 (11) | 13 | > 9 → right-- |
| 3 | 0 (2) | 1 (7) | 9 | == 9 → return [1, 2] ✓ |
Animated walkthrough
Use Next or Autoplay to see how the left and right pointers move based on the sum comparison.
Pseudocode
Section titled “Pseudocode”function twoSum(numbers, target): left, right = 0, len(numbers) - 1 while left < right: s = numbers[left] + numbers[right] if s == target: return [left + 1, right + 1] elif s < target: left++ else: right--Solution Code
Section titled “Solution Code”from typing import List
def two_sum(numbers: List[int], target: int) -> List[int]: left, right = 0, len(numbers) - 1 while left < right: s = numbers[left] + numbers[right] if s == target: return [left + 1, right + 1] elif s < target: left += 1 else: right -= 1 return []#include <vector>using namespace std;
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int left = 0, right = (int)numbers.size() - 1; while (left < right) { int s = numbers[left] + numbers[right]; if (s == target) return {left + 1, right + 1}; else if (s < target) left++; else right--; } return {}; }};class Solution { public int[] twoSum(int[] numbers, int target) { int left = 0, right = numbers.length - 1; while (left < right) { int s = numbers[left] + numbers[right]; if (s == target) return new int[]{left + 1, right + 1}; else if (s < target) left++; else right--; } return new int[]{}; }}function twoSum(numbers, target) { let left = 0, right = numbers.length - 1; while (left < right) { const s = numbers[left] + numbers[right]; if (s === target) return [left + 1, right + 1]; else if (s < target) left++; else right--; } return [];}impl Solution { pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> { let mut left = 0usize; let mut right = numbers.len() - 1; while left < right { let s = numbers[left] + numbers[right]; if s == target { return vec![(left + 1) as i32, (right + 1) as i32]; } else if s < target { left += 1; } else { right -= 1; } } vec![] }}func twoSum(numbers []int, target int) []int { left, right := 0, len(numbers)-1 for left < right { s := numbers[left] + numbers[right] if s == target { return []int{left + 1, right + 1} } else if s < target { left++ } else { right-- } } return []int{}}