This problem is similar to Two Sum, only difference is input array is now sorted.
Detailed problem
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,index1andindex2, 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.
classSolution {
publicint[]twoSum(int[] numbers, int target) {
int n = numbers.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (numbers[i]+ numbers[j]== target) {
returnnewint[]{i + 1, j + 1};
}
}
}
// This will never be reached because a solution is guaranteed.thrownew IllegalArgumentException("No solution found");
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
deftwoSum(self, numbers: List[int], target: int) -> List[int]:
n: int = len(numbers)
for i in range(n):
for j in range(i +1, n):
if numbers[i] + numbers[j] == target:
return [i +1, j +1]
# This line won't be reached as a solution is always guaranteed.raiseValueError("No solution exists")
classSolution {
publicint[]twoSum(int[] numbers, int target) {
int n = numbers.length;
for (int i = 0; i < n; i++) {
int required = target - numbers[i];
// Perform binary search in the right portion of the arrayint low = i + 1;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (numbers[mid]== required) {
returnnewint[]{i + 1, mid + 1}; // Return 1-based indices } elseif (numbers[mid]< required) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
// Guaranteed solution, so this won't executethrownew IllegalArgumentException("No solution found");
}
}
classSolution:
deftwoSum(self, numbers: List[int], target: int) -> List[int]:
n: int = len(numbers)
for i in range(n):
required: int = target - numbers[i]
# Binary search for the "required" value in the right portion of the array low: int = i +1 high: int = n -1while low <= high:
mid: int = (low + high) //2if numbers[mid] == required:
return [i +1, mid +1] # Return 1-based indiceselif numbers[mid] < required:
low = mid +1else:
high = mid -1# Guaranteed solution, so this won't executeraiseValueError("No solution exists")
⏰ Time complexity: O(n * log n). The outer loop runs n times, and for each iteration, binary search (O(log n)) is performed on the remaining portion of the array.
🧺 Space complexity: O(1). No extra data structures are used; everything is computed in-place.
classSolution {
publicint[]twoSum(int[] numbers, int target) {
int left = 0;
int right = numbers.length- 1;
while (left<right) {
int currSum = numbers[left]+ numbers[right];
if (currSum<target) {
++left;
} elseif (currSum > target) {
right--;
} else {
returnnewint[] {
left + 1, right + 1
};
}
}
returnnewint[]{-1, -1};
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
deftwoSum(self, numbers: List[int], target: int) -> List[int]:
l: int =0 r: int = len(numbers) -1while l < r:
sum: int = numbers[l] + numbers[r]
if sum == target:
return [l +1, r +1]
elif sum < target:
l +=1else:
r -=1# This line won't be reached as a solution is always guaranteed.return [-1, -1]