#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int BinSearch(const int* arr, int count, int key);
int main()
{
srand(time(NULL));
setlocale(0,"");
int n;
cout << "Input elements of array: ";
cin>>n;
int *arr = new int[n];
for(int i=0;i<n;i++)
arr[i]=rand()%10;
int key;
cout << "key: ";
cin >> key;
for(i=0;i<n;i++){
cout << arr[i] << endl;
}
if(BinSearch(arr, n, key) != -1)
cout << "Yes" << endl;
else cout << "No" << endl;
delete arr;
return 0;
}
int BinSearch(const int* arr, int count, int key)
{
int l = 0; // íèæíÿÿ ãðàíèöà
int u = count - 1; // âåðõíÿÿ ãðàíèöà
while (l <= u) {
int m = (l + u) / 2;
if (arr[m] == key) return m;
if (arr[m] < key) l = m + 1;
if (arr[m] > key) u = m - 1;
}
return -1;
}