Current streak:
0 days
Longest streak:
6 days
Less
More
int searchInsert(vector<int>& arr, int m)
{
// Write your code here.
int low=0;
int high=arr.size()-1;
int mid=low+(high-low)/2;
while(low<=high)
{
if(arr[mid]==m)
{
return mid;
}
else if(arr[mid]>m)
{
high=mid-1;
}
else{
low=mid+1;
}
mid=low+(high-low)/2;
}
return mid;
}
This is simple binary search code but i am updating my mid at last so whenever last so my mid got updated before the end of while loop and i return that mid
int searchInsert(vector<int>& arr, int m)
{
// Write your code here.
int low=0;
int high=arr.size()-1;
int mid=low+(high-low)/2;
while(low<=high)
{
if(arr[mid]==m)
{
return mid;
}
else if(arr[mid]>m)
{
high=mid-1;
}
else{
low=mid+1;
}
mid=low+(high-low)/2;
}
return mid;
}
This is simple binary search code but i am updating my mid at last so whenever last so my mid got updated before the end of while loop and i return that mid