fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int binary_search(int a[], int n, int x){
  4. int low = 0;
  5. int hight = n - 1;
  6. while(low <= hight){
  7. int mid = (low+hight)/2;
  8. if(x > a[mid]) low = mid +1;
  9. if(x < a[mid]) hight = mid - 1;
  10. else return(mid);
  11. }
  12. return -1;
  13. }
  14. int main(){
  15. int t;
  16. cin >> t;
  17. while(t--){
  18. int n;
  19. cin >> n;
  20. int a[n];
  21. for(int i = 0; i < n; i++)cin >> a[i];
  22. sort(a,a+n);
  23. int x;cin >> x; int result = binary_search(a,n,x);
  24. if(result != -1) cout << result << endl;
  25. else cout << "Not found!"<<endl;
  26. }
  27. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty