-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathbinary_search
43 lines (41 loc) · 898 Bytes
/
binary_search
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
#include<bits/stdc++.h>
#define mo 1000000007
#define ll long long int
#define pb push_back
#define pob pop_back
using namespace std;
ll binarySearch(ll arr[], ll l, ll r, ll x)
{
if (r >= l)
{
ll mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
void solve()
{
cout<<"Enter the number of elments in the array"<<"\n";
ll n;
cin>>n;
ll a[n];
cout<<"Enter the elments of the array in sorted order"<<"\n";
for(ll i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elments to be searched"<<"\n";
ll x;
cin>>x;
if(binarySearch(a,0,n-1,x)==-1)
cout<<"Element Not present"<<"\n";
else
cout<<"Element present in the array"<<"\n";
}
int main()
{
solve();
return 0;
}