forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinsearch.cpp
59 lines (46 loc) · 757 Bytes
/
binsearch.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
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
using namespace std;
int dec(int a)
{
return (a-2);
}
//return index if found else -1
// mid-1 should be +ive and mid -ive
// if mid +ive
int binSearch (int l,int h)
{
if(l<=h)
{
int mid=(l+h)/2;
if(dec(mid)<0 && dec(mid-1)>=0)
{
return mid;
}
else if(dec(mid)>0)
{
l=mid+1;
}
else
{
h=mid-1;
}
}
return -1;
}
int main()
{
vector <int> v;
int i,n;
cin>>n;
for(i=0;i<n;i++)
{
//cin>>k;
if(dec(i)<0)
{
cout<<"value of i required is "<<i;
}
}
}
// 3
// value of i required is 0value of i required is 1