Skip to content

Instantly share code, notes, and snippets.

@suryansh011
Created September 23, 2021 16:56
Show Gist options
  • Save suryansh011/ac5ccc9a8fc8253c55abdc683b5c73fe to your computer and use it in GitHub Desktop.
Save suryansh011/ac5ccc9a8fc8253c55abdc683b5c73fe to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main()
{
int i, count = 0, low, high, mid, n, key, array[100];
scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
count++;
if(array[mid] < key) {
low = mid + 1;
}
else if (array[mid] == key) {
printf("%d %d", mid+1, count);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high) {
printf("-1");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment