-
Notifications
You must be signed in to change notification settings - Fork 169
/
allocate_min_pages.cpp
79 lines (72 loc) · 1.96 KB
/
allocate_min_pages.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
/*
Problem Link: https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages0937/1#
*/
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
// helper function: greedy approach: give max possible load to each student
bool isPossible(int *arr, int n, int m, int cur_min) {
int students = 1;
int pages_read = 0;
for(int i = 0; i < n; ) {
// assign pages to the current student
if(pages_read + arr[i] <= cur_min) {
pages_read += arr[i];
i++;
}
else {
// assign pages to the new student;
students++;
pages_read = 0;
// condition check
if(students > m)
return false;
}
}
return true;
}
int findPages(int arr[], int n, int m) {
//code here
// edge case: students are more than the number of books
if(m > n)
return -1;
int result = INT_MAX;
// count the total number of pages
int sum = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
// apply binary search
int start = *max_element(arr, arr + n);
int end = sum;
while(start <= end) {
int mid = start + (end - start) / 2;
// each student reads atmost mid number of pages [max upper_bound]
if(isPossible(arr, n, m, mid)) {
result = min(result, mid);
end = mid - 1;
}
else
start = mid + 1;
}
return result;
}
};
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int A[n];
for(int i=0;i<n;i++){
cin>>A[i];
}
int m;
cin>>m;
Solution ob;
cout << ob.findPages(A, n, m) << endl;
}
return 0;
}