fork download
  1.  
  2. #include <stdio.h>
  3. int main() {
  4. int arr[] = {1,3,5,7,9};
  5. int l = 0, r = 4, x = 6, ans = -1;
  6. while (l <= r) {
  7. int m = (l + r) / 2;
  8. if (arr[m] < x) l = m + 1;
  9. else if (arr[m] > x) r = m - 1;
  10. else { ans = m; break; }
  11. }
  12. printf("%d\n", ans);
  13.  
  14. return 0;
  15. }
  16.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
-1