Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ull unsigned long long
- #define ld long double
- #define vi vector<int>
- #define vll vector<long long>
- #define pb push_back
- #define p_b pop_back
- using namespace std;
- int Binary_Search(int arr[], int s, int e, int x){
- if(e >= s){
- int mid = s+(e-s)/2;
- if(arr[mid] == x){
- return mid;
- } else if(arr[mid] > x){
- return Binary_Search(arr, s, mid-1, x);
- } else{
- return Binary_Search(arr, mid+1, e, x);
- }
- } else{
- return -1;
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int n; cin >> n;
- int arr[n];
- for(int i = 0; i < n; i++) cin >> arr[i];
- int key; cin >> key;
- cout << Binary_Search(arr, 0, n-1, key) << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement