-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary search.cpp
125 lines (114 loc) · 3.11 KB
/
binary search.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
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
122
123
124
125
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
int last_occurance(vector <int> arr,int x)
{
int low=0;
int high=arr.size()-1;
int mid=(low+high)/2;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==x && arr[mid+1]!=x) return mid;
else if(arr[mid]<=x) low=mid+1;
else high =mid-1;
}
return high;
}
int ones(vector<int> arr)
{
int low=0;
int high=arr.size()-1;
int mid=(low+high)/2;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==1 && arr[mid-1]==0) return arr.size()-mid;
else if(arr[mid]<=0) low=mid+1;
else high =mid-1;
}
return arr.size()-high;
}
int repeated_number(vector<int> arr)
{
int low=0;
int high=arr.size()-1;
int mid=(low+high)/2;
while(low<=high){
mid=(low+high)/2;
if((arr[mid]==arr[mid-1]) || (arr[mid]==arr[mid+1])) return arr[mid];
else if(arr[mid]==mid+1) low=mid+1;
else high =mid-1;
}
return arr[high];
}
string perfect_squre(int n)
{
int low=0;
int high=n;
int mid=(low+high)/2;
while(low<=high){
mid=(low+high)/2;
if(mid*mid==n) return "yes";
else if(mid*mid<n) low=mid+1;
else high =mid-1;
}
return "no";
}
vector<int> searchRange(vector<int>& arr, int x) {
int start=-1;
int low=0;
int high=arr.size()-1;
int mid;
//start
while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==x){
if(mid==0 || arr[mid-1]!=x){
start=mid;
break;
}
else high=mid-1;
}
if(arr[mid]<=x) low=mid+1;
else high=mid-1;
}
//end
int end=-1;
low=0;
high=arr.size()-1;
while(low<=high){
mid=low+(high-low)/2;
if(mid==(arr.size()-1) || arr[mid]==x){
if(arr[mid+1]!=x){
end=mid;
break;
}
else low=mid+1;
}
if(arr[mid]<=x) low=mid+1;
else high=mid-1;
}
end=low;
arr.clear();
arr.push_back(start);
arr.push_back(end);
return arr;
}
int main()
{
// vector <int> arr={1,2,3,3,4,4,4,4,4,4,4,4,4,5};
// int x=3;
// cout << last_occurance(arr,x);
// vector<int> arr={0,0,0,0,0,1,1,1,1};
// cout<< ones(arr);
// vector<int> arr={1,1,2,3,4,5,6};
// cout<< repeated_number(arr);
// int x=45;
// cout<< perfect_squre(x);
// vector<int> arr={5,7,7,8,8,10};
// int x=8;
// arr.clear();
// arr=searchRange(arr, x);
// cout<<arr[0]<<" \n"<<arr[1]<<endl;
return 0;
}