Advertisement
Josif_tepe

Untitled

Mar 4th, 2021
112
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 <queue>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int n;
  9.     cin >> n;
  10.     int a[n];
  11.     for(int i = 0; i < n; i++) {
  12.         cin >> a[i];
  13.     }
  14.     int x;
  15.     cin >> x;
  16.     int L = 0;
  17.     int R = n - 1;
  18.     while(L <= R) {
  19.         int middle = (L + R) / 2;
  20.         if(a[middle] < x) {
  21.             L = middle + 1;
  22.         }
  23.         else if(a[middle] > x) {
  24.             R = middle - 1;
  25.         }
  26.         else if(a[middle] == x) {
  27.             cout << "Ovoj broj postoi vo nizata" << endl;
  28.             return 0;
  29.         }
  30.        
  31.     }
  32.     cout << "Ne postoi" << endl;
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement