Advertisement
moisey312

Untitled

Mar 2nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string binary(int start, int end, vector<int> a, int search) {
  8.  
  9.     if ((start == end && a[start] != search) || start > end) {
  10.         return "NO";
  11.     } else {
  12.         int ser = (start + end) / 2;
  13.         if (a[ser] > search) {
  14.             return binary(start, ser - 1, a, search);
  15.         } else if (a[ser] < search) {
  16.             return binary(ser + 1, end, a, search);
  17.         } else {
  18.             return "YES";
  19.         }
  20.     }
  21. }
  22.  
  23. int main() {
  24.     int n, k, t;
  25.     cin >> n;
  26.     cin >> k;
  27.     vector<int> a(n);
  28.     for (int i = 0; i < n; i++) {
  29.         cin >> a[i];
  30.     }
  31.     for (int i = 0; i < k; i++) {
  32.         cin >> t;
  33.         cout << binary(0, n - 1, a, t) << endl;
  34.     }
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement