-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarysearch
More file actions
121 lines (105 loc) · 1.85 KB
/
Binarysearch
File metadata and controls
121 lines (105 loc) · 1.85 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
[l,r] implementation following-- So choose lower and upper bound respectively
//No No No Yes Yes Yes type question
//1. we need to find first yes ( I f function given increasing always use BS) Tip - use proper lower and upper bounds
bool check(lli ans)
{
}
lli l=1,h=LONG_LONG_MAX;
while(l<h)
{
lli m=l+(h-l)/2;
if(check(m))
{
h=m;
}
else
{
l=m+1;
}
}
if(!check(l))
cout<<"All are false"<<endl;
else
cout<<l<<endl;
//2. we need to find first no ( If function given monotonicity always use BS) Tip - use proper lower and upper bounds
bool check(lli ans)
{
}
lli l=1,h=LONG_LONG_MAX;
while(l<h)
{
lli m=l+(h-l)/2;
if(check(m))
{
h=m-1;
}
else
{
l=m;
}
}
if(!check(l))
cout<<"All are true"<<endl;
else
cout<<l<<endl;
//Yes Yes Yes No No Notype question
//1. we need to find last yes . Tip - use proper lower and upper bounds( case to find max which satisfy)
bool check(lli ans)
{
}
lli l=1,h=LONG_LONG_MAX;
while(l<h)
{
lli m=l+(h-l)/2;
if(check(m))
{
l=m;
}
else
{
h=m-1;
}
}
if(!check(l))
cout<<"All are false"<<endl;
else
cout<<l<<endl;
//2. we need to find first no ( If function given monotonicity always use BS) Tip - use proper lower and upper bounds
bool check(lli ans)
{
}
lli l=1,h=LONG_LONG_MAX;
while(l<h)
{
lli m=l+(h-l)/2;
if(check(m))
{
l=m+1;
}
else
{
h=m;
}
}
if(!check(l))
cout<<"All are true"<<endl;
else
cout<<l<<endl;
//// Binary search for double values --
const float target = 0.0000001;
//value to find
int main()
{
double l=0.00000000,r=100000.00000000;
int iterations = 0;
while(iterations < 300)
{
double mid = (float)((l+r)/2);
if(mid>target)
r=mid;
else
l=mid;
iterations++;
}
cout << l;
}