Challenge - 5 Problems
Allocate Minimum Pages Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Minimum Pages Allocation for Given Books
What is the output of the following code that finds the minimum number of pages to allocate to 2 students from the given books array?
DSA Python
def is_possible(arr, n, m, mid): student_count = 1 page_sum = 0 for i in range(n): if arr[i] > mid: return False if page_sum + arr[i] > mid: student_count += 1 page_sum = arr[i] if student_count > m: return False else: page_sum += arr[i] return True def find_pages(arr, n, m): start = max(arr) end = sum(arr) result = end while start <= end: mid = (start + end) // 2 if is_possible(arr, n, m, mid): result = mid end = mid - 1 else: start = mid + 1 return result books = [12, 34, 67, 90] students = 2 print(find_pages(books, len(books), students))
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the maximum pages a student can get.
✗ Incorrect
The minimum maximum pages allocated to 2 students is 113. The allocation can be [12, 34, 67] and [90], where the maximum pages assigned is 113.
🧠 Conceptual
intermediateUnderstanding the Role of 'mid' in Binary Search Allocation
In the binary search approach to allocate minimum pages, what does the variable 'mid' represent?
Attempts:
2 left
💡 Hint
Consider what the binary search is trying to minimize or maximize.
✗ Incorrect
'mid' is the candidate maximum pages that a student can be assigned. The algorithm checks if allocation is possible without exceeding 'mid'.
🔧 Debug
advancedIdentify the Error in Allocation Check Function
What error will the following code produce when running the allocation check for books = [10, 20, 30], students = 2, mid = 25?
DSA Python
def is_possible(arr, n, m, mid): student_count = 1 page_sum = 0 for i in range(n): if arr[i] > mid: return False if page_sum + arr[i] >= mid: student_count += 1 page_sum = arr[i] if student_count > m: return False else: page_sum += arr[i] return True print(is_possible([10, 20, 30], 3, 2, 25))
Attempts:
2 left
💡 Hint
Check the condition that triggers a new student allocation.
✗ Incorrect
The condition 'page_sum + arr[i] >= mid' causes the allocation to split too early, making it impossible to allocate within mid=25, so it returns False.
❓ Predict Output
advancedOutput of Allocation with Multiple Students and Books
What is the output of the code below for books = [10, 20, 30, 40, 50], students = 3?
DSA Python
def is_possible(arr, n, m, mid): student_count = 1 page_sum = 0 for i in range(n): if arr[i] > mid: return False if page_sum + arr[i] > mid: student_count += 1 page_sum = arr[i] if student_count > m: return False else: page_sum += arr[i] return True def find_pages(arr, n, m): start = max(arr) end = sum(arr) result = end while start <= end: mid = (start + end) // 2 if is_possible(arr, n, m, mid): result = mid end = mid - 1 else: start = mid + 1 return result books = [10, 20, 30, 40, 50] students = 3 print(find_pages(books, len(books), students))
Attempts:
2 left
💡 Hint
Try to split books into 3 groups minimizing the maximum pages per student.
✗ Incorrect
The minimum maximum pages is 60 with allocation like [10,20,30], [40], [50].
🧠 Conceptual
expertWhy Binary Search is Used in Allocate Minimum Pages Problem
Why is binary search an efficient approach to solve the Allocate Minimum Pages problem instead of a brute force approach?
Attempts:
2 left
💡 Hint
Think about how the maximum pages range is bounded and how binary search narrows it down.
✗ Incorrect
The maximum pages a student can get lies between the max single book pages and the sum of all pages. Binary search efficiently narrows this range by checking feasibility.
