fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6. int N = 5;
  7. int arr[5] = {3, 5, 8, 15, 19};
  8. int x = 18;
  9. int ans = 5;
  10.  
  11. int low = 0;
  12. int high = N-1;
  13.  
  14. while(low <= high){
  15. int mid = (low+high)/2;
  16.  
  17. if(arr[mid] >= x){
  18. ans = mid;
  19. high = mid - 1;
  20. }else{
  21. low = mid + 1;
  22. }
  23. }
  24.  
  25. cout<<ans<<endl;
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
4