fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, x;
  6. cin >> n;
  7.  
  8. vector<int> a(n);
  9. for(int i = 0; i < n; i++) {
  10. cin >> a[i];
  11. }
  12. cin >> x;
  13. int l = 0, r = n - 1;
  14. while(l <= r) {
  15. int m = (l + r) / 2;
  16.  
  17. if(a[m] == x) {
  18. cout << m << endl;
  19. return 0;
  20. }
  21. if(a[l] <= a[m]) {
  22. if(x >= a[l] && x < a[m])
  23. r = m - 1;
  24. else
  25. l = m + 1;
  26. }
  27. else {
  28. if(x > a[m] && x <= a[r])
  29. l = m + 1;
  30. else
  31. r = m - 1;
  32. }
  33. }
  34.  
  35. cout << -1 << endl;
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 5316KB
stdin
7
4 5 6 7 0 1 2
3


stdout
-1