-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookAllocation.cpp
66 lines (53 loc) · 1.99 KB
/
bookAllocation.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
#include<iostream>
#include<vector>
using namespace std;
// Function to check if it is possible to allocate books such that no student reads more than maxAllowedPages
bool isValid(vector<int> &arr, int n, int m, int maxAllowedPages) {
int students = 1, pages = 0;
for (int i = 0; i < n; i++) {
// If a single book has more pages than maxAllowedPages, return false
if (arr[i] > maxAllowedPages) {
return false;
}
// If adding this book to the current student's allocation doesn't exceed maxAllowedPages
if (pages + arr[i] <= maxAllowedPages) {
pages += arr[i];
} else {
// Allocate books to the next student
students++;
pages = arr[i];
}
}
// Return true if the number of students required is less than or equal to m
return students > m ? false : true;
}
// Function to allocate books such that the maximum number of pages assigned to a student is minimized
int allocateBooks(vector<int> &arr, int n, int m) {
// If there are fewer books than students, return -1
if(n < m) return -1;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
int ans = -1;
int st = 0, end = sum; // range of possible answers
// Binary search to find the minimum possible value of the maximum number of pages
while (st <= end) {
int mid = st + (end - st) / 2;
// Check if it is possible to allocate books with mid as the maximum number of pages
if (isValid(arr, n, m, mid)) {
ans = mid;
end = mid - 1; // Try for a smaller value in the left half
} else {
st = mid + 1; // Try for a larger value in the right half
}
}
return ans;
}
int main() {
vector<int> arr = {12, 34, 67, 90};
int n = arr.size();
int m = 2; // Number of students
cout << "The minimum possible value of the maximum number of pages is " << allocateBooks(arr, n, m) << endl;
return 0;
}