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 and Students
What is the output of the following Java code that uses binary search to allocate minimum pages to students?
DSA Java
public class Solution { public static boolean isPossible(int[] books, int students, int maxPages) { int count = 1, sum = 0; for (int pages : books) { if (pages > maxPages) return false; if (sum + pages > maxPages) { count++; sum = pages; if (count > students) return false; } else { sum += pages; } } return true; } public static int findPages(int[] books, int students) { int start = 0, end = 0; for (int pages : books) end += pages; int result = -1; while (start <= end) { int mid = start + (end - start) / 2; if (isPossible(books, students, mid)) { result = mid; end = mid - 1; } else { start = mid + 1; } } return result; } public static void main(String[] args) { int[] books = {12, 34, 67, 90}; int students = 2; System.out.println(findPages(books, students)); } }
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the maximum pages per student.
✗ Incorrect
The minimum maximum pages allocated to 2 students for books {12,34,67,90} is 113. The allocation can be (12,34,67) and (90), where the max pages is 113.
❓ Predict Output
intermediateResult of Allocation Check Function
What does the isPossible function return for books = {10, 20, 30, 40} with students = 2 and maxPages = 50?
DSA Java
public static boolean isPossible(int[] books, int students, int maxPages) { int count = 1, sum = 0; for (int pages : books) { if (pages > maxPages) return false; if (sum + pages > maxPages) { count++; sum = pages; if (count > students) return false; } else { sum += pages; } } return true; } // Call: isPossible(new int[]{10,20,30,40}, 2, 50)
Attempts:
2 left
💡 Hint
Check if books can be divided into 2 students without exceeding 50 pages each.
✗ Incorrect
The books can be allocated as (10,20,30) and (40). The first student gets 60 pages which is more than 50, so allocation fails. But wait, sum + pages > maxPages triggers new student allocation. Actually, allocation is (10,20) and (30,40) with max 50 pages each, so returns true.
🔧 Debug
advancedIdentify the Error in Binary Search Allocation Code
What error will occur when running this code snippet for allocating minimum pages?
DSA Java
public static int findPages(int[] books, int students) { int start = 0, end = 0; for (int pages : books) end += pages; int result = -1; while (start < end) { int mid = start + (end - start) / 2; if (isPossible(books, students, mid)) { result = mid; end = mid - 1; } else { start = mid + 1; } } return result; }
Attempts:
2 left
💡 Hint
Check the binary search loop condition and how start and end are updated.
✗ Incorrect
The loop condition 'while (start < end)' with 'end = mid - 1' can cause start to never reach end if mid equals start, causing infinite loop.
❓ Predict Output
advancedOutput of Allocation with Large Book Pages
What is the output of the minimum pages allocation for books = {100, 200, 300, 400, 500} and students = 3?
DSA Java
public static void main(String[] args) {
int[] books = {100, 200, 300, 400, 500};
int students = 3;
System.out.println(findPages(books, students));
}
// Uses previously defined findPages and isPossible methodsAttempts:
2 left
💡 Hint
Try to split books into 3 students minimizing the maximum pages.
✗ Incorrect
The allocation can be (100,200,300), (400), (500) with max pages 700, which is minimum possible.
🧠 Conceptual
expertWhy Binary Search is Used in Allocate Minimum Pages Problem?
Which reason best explains why binary search is used to solve the Allocate Minimum Pages problem?
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the possible maximum pages.
✗ Incorrect
Binary search is used on the range of possible maximum pages. For each mid value, feasibility is checked. This reduces time complexity.
