fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7.  
  8. vector<int> arr;
  9.  
  10. for(int i=0; i<n; i++) {
  11. int num;
  12. cin >> num;
  13.  
  14. arr.push_back(num);
  15. }
  16.  
  17. sort(arr.begin(), arr.end());
  18.  
  19. auto mg = upper_bound(arr.begin(), arr.end(), 35);
  20.  
  21. auto idx = mg - arr.begin(); // returns the index of number slightly greater then 100 in array
  22. cout << "Index: " << idx << endl;
  23.  
  24. int lb = arr[idx]; // Upper bound number
  25.  
  26. cout << "Nearest number: " << lb << endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
10
20
30
40
50
stdout
Index: 3
Nearest number: 40