fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7.  
  8. vector<int> arr = {3,5,5,8,8,10,12};
  9.  
  10. int ub = 6;
  11.  
  12. int ans =0;
  13. int st=0, end=arr.size()-1;
  14.  
  15. while( st<=end){
  16.  
  17. int m = st+ (end-st)/2;
  18.  
  19. if(arr[m]<=ub){
  20. st=m+1;
  21. }
  22. else{
  23. if(m==0){
  24. ans=m;
  25. break;
  26. }
  27. else if(arr[m-1]<ub){
  28. ans = m;
  29. break;
  30. }
  31. else{
  32. end = m-1;
  33. }
  34. }
  35. }
  36.  
  37. cout<< ans;
  38.  
  39. auto mg = upper_bound(arr.begin(), arr.end(), ub);
  40. cout<< endl << *mg;
  41. cout<< endl << *arr.begin();
  42. cout << endl<< mg - arr.begin();
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
3
8
3
3