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. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
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.
Example 1:
Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:
Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Example 3:
Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
Constraints:
2 <= numbers.length <= 3 * 104-1000 <= numbers[i] <= 1000numbers is sorted in non-decreasing order.-1000 <= target <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a row of numbers arranged in order from smallest to largest, and you need to find two distinct numbers from that row that add up to a specific target value. The brute force approach involves trying every single possible pair of numbers in the row until you find the pair that hits the target.
Here's how the algorithm would work step-by-step:
def two_sum(numbers, target):
# Iterate through every possible first number to test all unique pairs sequentially
for first_pointer in range(len(numbers)):
# Test the current number against every subsequent number in the sorted array
for second_pointer in range(first_pointer + 1, len(numbers)):
# Check if the sum of the chosen pair exactly matches the required target value
if numbers[first_pointer] + numbers[second_pointer] == target:
return [first_pointer + 1, second_pointer + 1]
return []Since the items are already arranged neatly from smallest to largest, we can find our target pair much faster without testing every single combination. By placing one helper at the very beginning and another at the very end, we can intelligently narrow down our search based on how their combined value compares to our target.
Here's how the algorithm would work step-by-step:
class Solution:
def twoSum(self, numbers_array: list[int], target_value: int) -> list[int]:
left_pointer = 0
right_pointer = len(numbers_array) - 1
while left_pointer < right_pointer:
current_sum = numbers_array[left_pointer] + numbers_array[right_pointer]
# We found the exact pair matching the target sum.
if current_sum == target_value:
return [left_pointer + 1, right_pointer + 1]
# The sum is too small, so advance the left pointer to increase the total.
elif current_sum < target_value:
left_pointer += 1
# The sum is too large, so retreat the right pointer to decrease the total.
else:
right_pointer -= 1
return []| Case | How to Handle |
|---|---|
| Array with exactly two elements | The two pointers start at index 0 and 1, correctly evaluating the only possible pair immediately. |
| Negative numbers and zero included in the array | The two-pointer technique works mathematically with any signed integers because the array is sorted. |
| All elements are identical | Pointers will correctly converge on the exact two indices whose sum matches the target. |
| Maximum-sized inputs with values near integer limits | Addition could theoretically overflow standard integer bounds, so using 64-bit integers prevents overflow. |
| Target can only be met by the very first and very last elements | The left and right pointers start at opposite ends and will evaluate this extreme span on the first iteration. |
| Target can only be met by adjacent elements in the middle of the array | The pointers will systematically narrow down from the edges until they meet at the correct adjacent pair. |
| Problem statement guarantees exactly one solution | We do not need to handle multiple solutions or missing pairs, allowing the search to terminate instantly upon a match. |
| Inputs using 1-based indexing as specified | We must add 1 to the final 0-indexed array positions before returning the result. |