-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_search.cpp
49 lines (38 loc) · 872 Bytes
/
binary_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ll long long
#define endl "\n"
#define pf first
#define ps second
#define loop(i, a, b) for(int i = a; i < b; i++)
#define rev_loop(i, a, b) for(int i = a-1; i >= 0; i--)
ll binary_search() {
int n, key, result; cin >> n;
vector<int>v(n);
for(int &i : v) cin >> i;
sort(v.begin(), v.end());
cin >> key;
int l = 0 , h = n-1 ;
while (h - l > 1) {
int m = ( h + l ) / 2;
if( v[m] < key ) l = m + 1;
else h = m;
}
if(v[l] == key) result = l + 1;
else if(v[h] == key) result = h + 1;
else result = -1; // when value not found return -1
return result;
}
void solve(){
cout << binary_search() << endl ;
}
signed main() {
fast;
ll tc = 1;
//cin >> tc; // when test case needed
while (tc--){
solve();
}
return 0;
}