-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookAllocation.cpp
54 lines (54 loc) · 1.05 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
#include<iostream>
#include<vector>
using namespace std;
bool isPossible(vector<int> &arr, int n, int m, int mid)
{
int studCount = 1;
int pageSum = 0;
for (int i=0;i<n;i++)
{
if (pageSum + arr[i] <= mid)
{
pageSum = pageSum + arr[i];
}
else
{
studCount++;
if (studCount > m || arr[i] > mid)
{
return false;
}
pageSum = arr[i];
}
}
return true;
}
int main()
{
vector <int> arr = {10,20,30,40}; //Pages in each Book
int n = 4; // Number of books
int m = 2; // Nmber of students
int start = 0;
int sum = 0;
for (int i=0;i<n;i++)
{
sum=sum+arr[i];
}
int end = sum;
int ans = -1;
int mid = (start+end)/2;
while (start<=end)
{
if(isPossible(arr, n, m ,mid))
{
ans = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
mid = start + (end-start)/2;
}
cout<<ans;
}