-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocateMinimumNumberOfPages.py
More file actions
44 lines (37 loc) · 995 Bytes
/
AllocateMinimumNumberOfPages.py
File metadata and controls
44 lines (37 loc) · 995 Bytes
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
n=int(input())
m=int(input())
arr=list(map(int,input().split()))
# This function checks whether the given minimum pages by binary search are possible to be distributed with current number of students or not
def curr_min(arr,n,m,x):
stud = 1
curr_sum = 0
for i in range(n):
if arr[i]>x:
return False
if curr_sum + arr[i] > x:
stud +=1
curr_sum = arr[i]
if stud>m:
return False
else:
curr_sum += arr[i]
return True
# Used Binary Search to find the minimum number of Pages to be read
def bin_search(arr,n,m):
sum1=0
if n<m:
return -1
for i in range(n):
sum1 += arr[i]
start = 0
end = sum1
result = 10**9
while start<=end:
mid = (start+end)//2
if curr_min(arr,n,m,mid):
result = min(result,mid)
end = mid-1
else:
start = mid+1
return result
print(bin_search(arr,n,m))