Josif_tepe

Untitled

Nov 5th, 2025
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. //#include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. const int maxn = 1e5 + 10;
  7.  
  8. int main()
  9. {
  10.     int n, x;
  11.     cin >> n >> x;
  12.    
  13.     int a[n];
  14.    
  15.     for(int i = 0; i < n; i++) {
  16.         cin >> a[i];
  17.     }
  18.    
  19.     int L = 0, R = n - 1;
  20.     while(L <= R) {
  21.         int middle = (L + R) / 2;
  22.        
  23.         if(x == a[middle]) {
  24.             cout << "Found at: " << middle << endl;
  25.             break;
  26.         }
  27.        
  28.         if(x < a[middle]) {
  29.             R = middle - 1;
  30.         }
  31.         else {
  32.             L = middle + 1;
  33.         }
  34.     }
  35.    
  36.    
  37.    
  38.    
  39.      return 0;
  40. }
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment