-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBook_Allocation.cpp
69 lines (66 loc) · 1.25 KB
/
Book_Allocation.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
// Implemented by Kritagya Kumra
#include <iostream>
using namespace std;
bool ispossible(int arr[], int barrier, int n, int students)
{
int allocated_student = 1, pages = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > barrier)
{
return false;
}
if (pages + arr[i] > barrier)
{
allocated_student += 1;
pages += arr[i];
}
else
{
pages += arr[i];
}
}
if (allocated_student > students)
{
cout << pages;
return true;
}
else
{
cout << pages;
return false;
}
}
int sum(int arr[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
{
ans += i;
}
return ans;
}
int main()
{
int arr[4] = {12, 34, 67, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int start = arr[0], end = sum(arr, n);
cout << start << " " << end;
int mid = start + (end - start) / 2;
int res = -1;
while (start <= end)
{
if (ispossible(arr, mid, n, 2))
{
res = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
}
cout << start;
return 0;
}
// Implemented by Kritagya Kumra