-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBinary_Search.cpp
More file actions
88 lines (69 loc) · 1.32 KB
/
Binary_Search.cpp
File metadata and controls
88 lines (69 loc) · 1.32 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
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Stores the frequency of
// each type of chocolate
map<int, int> mp;
int N, P;
// Function to check if chocolates
// can be eaten for 'mid' no. of days
bool helper(int mid)
{
int cnt = 0;
for (auto i : mp) {
int temp = i.second;
while (temp >= mid) {
temp -= mid;
cnt++;
}
}
// If cnt exceeds N,
// return true
return cnt >= N;
}
// Function to find the maximum
// number of days for which
// chocolates can be eaten
int findMaximumDays(int arr[])
{
// Store the frequency
// of each type of chocolate
for (int i = 0; i < P; i++) {
mp[arr[i]]++;
}
// Initialize start and end
// with 0 and P respectively
int start = 0, end = P, ans = 0;
while (start <= end) {
// Calculate mid
int mid = start
+ ((end - start) / 2);
// Check if chocolates can be
// distributed for mid days
if (mid != 0 and helper(mid)) {
ans = mid;
// Check if chocolates can
// be distributed for more
// than mid consecutive days
start = mid + 1;
}
else if (mid == 0) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return ans;
}
// Driver code
int main()
{
N = 3, P = 10;
int arr[] = { 1, 2, 2, 1, 1,
3, 3, 3, 2, 4 };
// Function call
cout << findMaximumDays(arr);
return 0;
}