-
Notifications
You must be signed in to change notification settings - Fork 0
/
STLBINARYSEARCH.cpp
62 lines (44 loc) · 1.5 KB
/
STLBINARYSEARCH.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
60
61
62
// ALL WILL WORK IN LOG(N)
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 1. check if X exists in sorted array or not?
// a[]={1,4,5,8,9}
// bool res=binary_search(a,a+n,3);
// not found
// bool res=binary_search(a,a+n,4);
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// a[]={1,4,5,6,9,9}
// int id=lower_bound(a,a+n,4)-a; - 1
// int id=lower_bound(a,a+n,7)-a; - 4
// int id=lower_bound(a,a+n,10)-a; - 6
// in id=lower_bound(a.begin(),a.end(),x)-a.begin();
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// int id=upper_bound(a,a+n,4) - a; - 2
// int id=lower_bound(a,a+n,7)-a; - 4
// int id=lower_bound(a,a+n,10)-a; - 6
// -----------------------------------------------
// FIRST OCCURENCE
// a[]={1,4,4,4,4,9,9,10,11}
// int ind=lower_bound(a,a+n,X);
// if(ind!=n && a[ind]==X) cout<<ind;
// else count<<-1;
// --------------------------------------------------
// 8888888888888888888888888888888888888888888888888888
// LAST OCCURENCE
// int ind=upper_bound(a,a+n,X);
// ind--;
// if(ind>=0 && a[ind]==X) cout<<ind;
// else count<<-1;
// 888888888888888888888888888888888888888888888888
// 000000000000000000000000000000000000000000000
// LARGEST NUMBER SMALLER THAN X
// int ind=lower_bound(a,a+n,X) -a;
// ind--;
// if(ind>=0)cout<<ind;
// else cout<<-1;
// 00000000000000000000000000000000000000000000
// 000000000000000000000000000000000000000
// Smallest number grater than X
// int ind=upper_bound(a,a+n,) - a;
// if(index<n)cout<<ind;
// else cout<<-1;
// 0000000000000000000000000000000000000000000