Advertisement
Josif_tepe

Untitled

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