-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOccurenceInArrayUsingBinarySearch.cpp
163 lines (105 loc) · 2.62 KB
/
OccurenceInArrayUsingBinarySearch.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream>
//Finding number of occurences of a number in an sorted array using Binary Search
using namespace std;
//Merge sort to sort the given array
void Merge(int arr[],int start,int mid , int end)
{
int i,j,k;
int nl,nr;
//number of elements in L and R arrays
nl = (mid-start) + 1;
nr = (end-mid);
mid = (start + (end -start)/2 );
//new arrays to store the left and right subarrays
int l[nl],r[nr];
//copying items to the subarrays from the orig array
for(i = 0 ; i < nl ; i++) {
l[i] = arr[start+i];
}
for(j= 0 ; j< nr ; j++) {
r[j] = arr[mid + 1 + j];
}
i=0,j=0,k=start;
while( i < nl && j < nr ) {
if(l[i] < r[j])
{
arr[k] = l[i];
i++;
}
else {
arr[k] = r[j];
j++;
}
k++;
}
//now copying the left overs in new sorted array
while( i < nl )
{
arr[k] = l[i];
i++;
k++;
}
while( j < nr )
{
arr[k] = r[j];
j++;
k++;
}
}
void MergeSort(int arr[],int start,int end) {
if(start < end) {
int mid = (start + (end - start)/2 ) ;
MergeSort(arr,start,mid);
MergeSort(arr,mid+1,end);
Merge(arr,start,mid,end);
}
}
//Binary search can also be used to find first and last occurences of a repeating number in an array
int BinarySearch(int arr[],int n , int x , bool searchFirst)
{
int l = 0;
int h = n-1;
int result = -1;
while(l <= h)
{
int mid = (l + (h-l)/2);
if(arr[mid]==x)
{
result = mid;
//if want to search for first occurence or a repeating no
if(searchFirst) {
//search only in the lower indices
h = mid-1;
}
//if want to search for the last occurence of a repeating number
else {
//search only in the higher indices or right subpart of array
l = mid + 1;
}
}
//search in higher indices or right subpart of array
else if(arr[mid] <= x) l = (mid + 1);
//search in lower indices or left subpart of array
else h = (mid-1);
}
return result;
}
int main () {
int arr [] = {15,10,2,9,2,8,15,2,3};
//sorting the array to apply binary search
MergeSort(arr, 0 , sizeof(arr)/sizeof(arr[0])-1 );
cout<<"Sorted array is:"<<endl;
for(int i = 0 ; i < sizeof(arr)/sizeof(arr[0]);i++) {
cout<<arr[i]<<endl;
}
//finding the index of the first occurecne of searched number
int firstOcc = BinarySearch(arr,sizeof(arr)/sizeof(arr[0]), 15 , true);
if(firstOcc==-1) {
cout<<"0 occurence."<<endl;
}
else {
//findind the index of the last occurence of the searched number
int lastOcc = BinarySearch(arr,sizeof(arr)/sizeof(arr[0]), 15 , false);
cout<<"Count of occurence is :"<<(lastOcc-firstOcc +1)<<endl;
}
}