forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number_of_duplicates_using_Binary_search.c
149 lines (121 loc) · 3.82 KB
/
Number_of_duplicates_using_Binary_search.c
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
/* To print the occurrence of the given number in the list/array using Binary Search Technique */
#include <stdio.h>
int Binary_search_algo_for__finding_occurrence(int* , int , int , int);
int Partition(int* , int , int);
int Quick_sort(int* , int , int);
int Binary_search_algo_for__finding_occurrence(int array[], int sizeOf_array, int number, int found)
{
int start = 0, end = sizeOf_array - 1;
// initializing value
int value = -1;
while (start <= end)
{
int middle = (start + end) / 2;
/*** CASE : number equals middle element ***/
if (number == array[middle]) // if both are equal then we update the 'value'
{
value = middle;
if (found) { // searching in left side
end = middle - 1;
}
else { // searching in right side
start = middle + 1;
}
}
/*** CASE : number less than middle element ***/
else if (number < array[middle])
{
end = middle - 1;
}
/*** CASE : number greater than middle element (last case) ***/
else
{
start = middle + 1;
}
}
return value;
}
int Partition(int array[], int lower_bound, int upper_bound)
{
int pivot , start , end , t;
pivot = array[lower_bound];
start = lower_bound ;
end = upper_bound ;
while (start < end)
{
while (array[start] <= pivot)
{
start++;
}
while (array[end] > pivot)
{
end--;
}
if (start < end)
{ //swap
t = array[start];
array[start] = array[end];
array[end] = t;
}
}
//if the condition is not followed then we replace the 'end' with 'pivot' element and return end
//swap
t = array[lower_bound];
array[lower_bound] = array[end];
array[end] = t ;
return end ;
}
int Quick_sort(int array[] , int lower_bound , int upper_bound)
{
int temp ;
if (lower_bound < upper_bound)
{
temp = Partition(array , lower_bound , upper_bound );// as partition function would return end index which would be used further
Quick_sort(array , lower_bound , temp - 1);
Quick_sort(array , temp + 1, upper_bound);
}
}
int main()
{
//declaring the size of array
int sizeOf_array;
printf("The size of array is:\n");
scanf("%d", &sizeOf_array);
// defining the array
int array[sizeOf_array];
printf("Please input the elements in array:\n");
// storing elements in array
for (int i = 0 ; i < sizeOf_array; i++) {
scanf("%d", &array[i]);
}
printf("\n");
int number; // we would input the number whose occurrence must be returned.
printf("The number whose occurrence must be found : ");
scanf("%d", &number);
// we would sort the array using Quick_sort
int lower_bound = 0 , upper_bound = sizeOf_array - 1;
Quick_sort(array , lower_bound , upper_bound);
// now we have sorted the array
// we pass 1 for the first occurrence of the number
int first_occurrence_of_number = Binary_search_algo_for__finding_occurrence(array, sizeOf_array, number, 1);
// we pass 0 for the first occurrence of the number
int last_occurrence_of_number = Binary_search_algo_for__finding_occurrence(array, sizeOf_array, number, 0);
// now we would subtract the first & last occurrence to get the total occurrences of the number
int total_occurrences_of_number = last_occurrence_of_number - first_occurrence_of_number + 1;
if (first_occurrence_of_number != -1) {
printf("The number: %d occurs %d times in the given array.", number, total_occurrences_of_number);
}
else {
printf("Error! The number is not present in the array.");
}
return 0;
}
/*
Sample Test Case
Input
5
1 4 2 3 4
4
Output
Occurrence of '4' : 2
*/