You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ans[1] = binarySearch(nums,target,false) -1; //Note that there is a minus one here because you did a mid + 1. And if so happen it is the last element. You need to go back.
return ans;
};
const binarySearch = (nums,target,left) =>{
let start = 0;
let end = nums.length;
while(start < end){
let mid = start + Math.floor((end - start)/2);
if(nums[mid] > target || (left && nums[mid] == target)){ //means target on the left