fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. int findUpperbound(vector<int> &arr , int key) {
  7. int s = 0 ;
  8. int e = arr.size()-1;
  9. int ans = -1;
  10.  
  11. while (s<=e) {
  12. int mid = s+(e-s)/2;
  13. if (arr[mid] > key) {
  14. ans = mid;
  15. e = mid-1;
  16. }
  17. else{
  18. s = mid+1;
  19. }
  20. }
  21. return ans;
  22. }
  23. int main() {
  24. // your code goes here
  25. int n;
  26. cin>>n;
  27. int x ;
  28. cin>>x;
  29. vector<int>arr(n);
  30.  
  31. for (int i = 0 ; i<n; i++) cin>>arr[i];
  32.  
  33. int ans = findUpperbound(arr , x);
  34. cout<< arr[ans];
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5288KB
stdin
6
4
1 2 4 4 6 8
stdout
6