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
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
/** * @param {number[]} nums * @param {number} target * @return {number} *///Due to the fact that "start" will always approach the target if "mid" did not match targetvarsearchInsert=function(nums,target){letstart=0,end=nums.length-1;while(start<=end){letmid=Math.floor((start+end)/2);if(nums[mid]===target){returnmid;}elseif(nums[mid]<target){start=mid+1;}elseif(nums[mid]>target){end=mid-1;}}returnstart;};
The text was updated successfully, but these errors were encountered:
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
The text was updated successfully, but these errors were encountered: