Advertisement
bero_0401

problem 3

Jul 8th, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main() {
  6.  
  7.     int n , x;
  8.     cin >> n >>x;
  9.     int a[n];
  10.     for (int i = 0; i < n; i++)cin >> a[i];
  11.  
  12.     int start = 0;
  13.     int end = n-1;
  14.     int ans = -1;
  15.    while(start <= end){
  16.        int mid = (start + end) / 2;
  17.        if(a[mid] <= x){
  18.            start = mid+1;
  19.            ans = a[mid];
  20.        }else if(a[mid] > x){
  21.            end = mid - 1;
  22.        }
  23.    }
  24.    cout<<ans<<"\n";
  25.     return 0;
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement