Given an array of integers nums
which is sorted in ascending order, and an integer target
, write a function to search target
in nums
. If target
exists, then return its index. Otherwise, return -1
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1
Constraints:
1 <= nums.length <= 104
-104 < nums[i], target < 104
nums
are unique.nums
is sorted in ascending order.This problem requires finding the index of a target integer within a sorted array. The most efficient approach is to use binary search, which has a time complexity of O(log n).
Algorithm:
Initialization: Set two pointers, left
(l) and right
(r), to the start and end of the array, respectively.
Iteration: While left
is less than right
:
mid
as (left + right) / 2
(or using bitwise shift (left + right) >> 1
for slightly better performance).nums[mid]
with the target
:
nums[mid]
is greater than or equal to target
, the target (if it exists) must be in the left half or at mid
. Therefore, update right
to mid
.left
to mid + 1
.Result: After the loop terminates (left
>= right
), there are two possibilities:
nums[left]
equals target
, return left
(the index of the target).-1
.Time Complexity: O(log n) because in each iteration, we are halving the search space.
Space Complexity: O(1) because we are using a constant amount of extra space (only the left
, right
, and mid
variables).
Code Examples in Multiple Languages:
Python:
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) >> 1 # Bitwise right shift for efficiency
if nums[mid] >= target:
right = mid
else:
left = mid + 1
return left if nums[left] == target else -1
Java:
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = (left + right) >> 1; // Bitwise right shift
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left] == target ? left : -1;
}
}
C++:
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = (left + right) >> 1; // Bitwise right shift
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left] == target ? left : -1;
}
};
JavaScript:
var search = function(nums, target) {
let left = 0, right = nums.length -1;
while(left < right){
let mid = Math.floor((left + right) / 2);
if(nums[mid] >= target){
right = mid;
} else {
left = mid + 1;
}
}
return nums[left] === target ? left : -1;
};
The other languages (Go, TypeScript, Rust, C#) follow a very similar structure, using the same core binary search algorithm with minor syntax adjustments specific to each language. The efficiency remains the same across all implementations.