Skip to content

Commit 2e562d9

Browse files
committed
Added binary search solution in c++
1 parent 6aecf61 commit 2e562d9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

binary_search/solution.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
#include <iostream>
3+
using namespace std;
4+
5+
bool binarySearch(vector <int> arr,int ele)
6+
{
7+
int l=0;
8+
int r=arr.size()-1;
9+
while(l<=r)
10+
{
11+
int m=(l+r)/2;
12+
if(arr[m]==ele)
13+
return true;
14+
else{
15+
if(arr[m]<ele)
16+
l=m+1;
17+
else r=m-1;
18+
}
19+
}
20+
21+
return false;
22+
}
23+
24+
int main()
25+
{
26+
vector<int> arr({1,2,3,4,5});
27+
int ele=1;
28+
if(binarySearch(arr,ele))
29+
cout<<"Exists"<<endl;
30+
else cout<<"Doesn't exist"<<endl;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)