fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int upper_bound1(vector<int> &arr, int target){
  5. int l = 0, r = arr.size() - 1;
  6. int pos = -1;
  7. while(l <= r){
  8. int mid = (r+l)/2;
  9. if(arr[mid] < target){
  10. l = mid+1;
  11. }
  12. else if(arr[mid] > target){
  13. pos = mid;
  14. r = mid-1;
  15. }
  16. }
  17.  
  18. return pos;
  19. }
  20.  
  21. int main() {
  22. // your code goes here
  23. vector<int> arr = {5, 8, 13, 15, 22, 29, 35, 38, 42};
  24. int result = upper_bound1(arr, 25);
  25. if(result != -1){
  26. cout << arr[result];
  27. }
  28. return 0;
  29. }
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 2: near "using": syntax error
Error: near line 4: near "int": syntax error
Error: near line 6: near "int": syntax error
Error: near line 7: near "while": syntax error
Error: near line 9: near "if": syntax error
Error: near line 11: unrecognized token: "}"
Error: near line 14: near "r": syntax error
Error: near line 15: unrecognized token: "}"
Error: near line 19: unrecognized token: "}"
Error: near line 24: near "int": syntax error
Error: near line 25: near "if": syntax error
Error: near line 27: unrecognized token: "}"
Error: near line 29: unrecognized token: "}"