DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
352 B

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std ;
  4. int main()
  5. {
  6. int n ;
  7. cin >> n ;
  8. int a[n] ;
  9. for(int i = 0 ; i < n ; ++i) cin >> a[i] ;
  10. sort(a,a+n) ;
  11. int x ;
  12. cin >> x ;
  13. int l = 0 , r = n ;
  14. while(r-l>1)
  15. {
  16. int mid = (r+l)/2 ;
  17. if(a[mid] <= x)
  18. l = mid ;
  19. else r = mid ;
  20. }
  21. if(a[l]==x)
  22. cout << l ;
  23. else
  24. cout << -1 ;
  25. }