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.
Time complexity : O(logN). Space COmplexity: O(N)
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let start = 0, end = nums.length - 1;
while(start <= end) {
let mid = Math.floor((start + end) / 2);
if(nums[mid] === target) {
return mid;
}
if(target < nums[mid]) {
end = mid - 1;
}
else {
start = mid + 1;
}
}
return -1;
};