#include <stdio.h> #include <stdint.h> int binary_search(uint8_t *arr, uint8_t n, uint8_t key) { // Your logic here uint8_t start=0,end=n,pos=0,i=0; while(true){ pos=(start+end)/2; if(arr[pos]==key){ return pos; } else if(arr[pos]>key){ end=pos-1; } else{ start=pos+1; } if(i>n){ return -1; } i++; } //return -1; } int main() { uint8_t n, key; scanf("%hhu", &n); uint8_t arr[100]; for (uint8_t i = 0; i < n; i++) { scanf("%hhu", &arr[i]); } scanf("%hhu", &key); int index = binary_search(arr, n, key); printf("%d", index); return 0; }
Test Cases
Test Results
Input
6 5 10 15 20 25 30 20
Expected Output
3