Advertisement
Josif_tepe

Untitled

Oct 12th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int main() {
  5.     int n;
  6.     cin >> n;
  7.    
  8.     int niza[n];
  9.     for(int i = 0; i < n; i++) {
  10.         cin >> niza[i];
  11.     }
  12.     sort(niza, niza + n);
  13.     int x;
  14.     cin >> x;
  15.    
  16.     int L = 0, R = n - 1;
  17.    
  18.     while(L <= R) {
  19.         int middle = (L + R) / 2;
  20.         if(x == niza[middle]) {
  21.             cout << "Brojot " << x << " postoi vo nizata" << endl;
  22.             break;
  23.         }
  24.         if(x < niza[middle]) {
  25.             R = middle - 1;
  26.         }
  27.         else {
  28.             L = middle + 1;
  29.         }
  30.     }
  31.    
  32.     return 0;
  33. }
  34.  // 972
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement