Initially, you are given N pages each of dimension n*m . each of these pages contains some amount of 1-1 squares drawn on it. you have the special power to shift a square from one page to another page and you want to minimise the number of pages used programming algorithm
Answers
Answer:
hut yahan se
Explanation:
i dunno why did i said thet but if you felt bad then srry
Answer:
Here is the Python algorithm for the above problem statement
Code:
def isPossible(arr, n, m, curr_min):
studentsRequired = 1
curr_sum = 0
for i in range(n):
if (arr[i] > curr_min):
return False
if (curr_sum + arr[i] > curr_min):
studentsRequired += 1
curr_sum = arr[i]
if (studentsRequired > m):
return False
else:
curr_sum += arr[i]
return True
def findPages(arr, n, m):
sum = 0
if (n < m):
return -1
for i in range(n):
sum += arr[i]
start, end = 0, sum
result = 10**9
while (start <= end):
mid = (start + end) // 2
if (isPossible(arr, n, m, mid)):
result = mid
end = mid - 1
else:
start = mid + 1
return result
arr = [12, 34, 67, 90]
n = len(arr)
m = 2 # No. of students
print("Minimum number of pages = ",findPages(arr, n, m))
Explanation:
- The concept is to employ Binary Search. We decide on a number of pages that is in the middle of the present minimum and maximum.
- Minimum and maximum are set to 0 and sum-of-all-pages, respectively. If a present mid can be a solution, we look in the lower half; otherwise, we look in the upper half.
#SPJ3