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
while(left<=right) //goal of this loop is to find the first appearance of target or minIndex
{
mid = Math.floor((left + right)/2)//finding the mid
if(arr[mid] === target){//if target is found
minIndex = Math.min(mid, minIndex) //storing the smallest of all the target's indexes
right = mid - 1 //shifting to left of mid to search target, as left has more minimal indexes
}
if(target < arr[mid])//If yes, shift the searching area to right of mid
{
right = mid - 1
}
if(target > arr[mid]){//If yes, shift the searching area to left of mid
left = mid + 1
}
}
return minIndex==9999999 ? undefined : arr[minIndex-1] //if minIndex was not changed then we did not found the element, thus returning undefined else we return the element just before the first appearance of target