Binary search algorithm variant

int binary_search(int arr[], int len, int target){
left = 0;
right = size - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(arr[mid] == target) return mid;
else if(arr[mid] > target) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return right + 1;
}
int binary_search(int arr[], int len, int target){int left = 0;int right = len;while(left < right) {int mid = left + (right - left) / 2;if(arr[mid] == target) return mid;else if(arr[mid] > target) left = mid + 1;else right = mid;}return left;}
int find_first_bad_version(int arr[], int len) {int left = 0;int right = len - 1;while(left < right) {int mid = (left + right) / 2;if(arr[mid] == 0) right = mid;else left = mid + 1;}return left;}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store