-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
executable file
·49 lines (43 loc) · 923 Bytes
/
BinarySearch.cpp
File metadata and controls
executable file
·49 lines (43 loc) · 923 Bytes
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 ll long long
#define lli long long int
#define vi vector<int>
#define forinput for(int i = 0; i<n ; i++){cin>>arr[i];}
#define foroutput for(int i = 0; i<n; i++){cout<<arr[i]<<" ";}
#define pb push_back
#define pob pop_back
int binarySearch(int arr[], int n, int ele){
int low = 0;
int high = n-1;
while(low <= high){
int mid = low + (high-low)/2;
if(arr[mid] == ele){
return mid;
}
else if(mid > ele){
high = mid - 1;
}
else{
low = mid + 1;
}
}
return 1;
}
int main(){
// "Enter the value of test cases you want "
int t; cin>>t;
while(t--){
// "Enter the size of array "
int n; cin>>n;
int arr[n];
// "Enter the array with n size separated by a single space "
forinput
// "Enter tHe Element you wanna Search "
int ele; cin>>ele;
int answer = binarySearch(arr, n, ele);
cout<<answer<<endl;
}
return 0;
}
// 9151666779