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 minimum number of pages allocated to a student when books are allocated optimally?
DSA C
int isPossible(int arr[], int n, int m, int mid) { int students = 1, pages_sum = 0; for (int i = 0; i < n; i++) { if (arr[i] > mid) return 0; if (pages_sum + arr[i] > mid) { students++; pages_sum = arr[i]; if (students > m) return 0; } else { pages_sum += arr[i]; } } return 1; } int findPages(int arr[], int n, int m) { int sum = 0, start = 0, end, result = -1; for (int i = 0; i < n; i++) sum += arr[i]; end = sum; while (start <= end) { int mid = (start + end) / 2; if (isPossible(arr, n, m, mid)) { result = mid; end = mid - 1; } else { start = mid + 1; } } return result; } #include <stdio.h> int main() { int arr[] = {12, 34, 67, 90}; int n = sizeof(arr) / sizeof(arr[0]); int m = 2; int result = findPages(arr, n, m); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Think about dividing books so that the maximum pages assigned to any student is minimized.
✗ Incorrect
The minimum maximum pages allocated is 113 when books are divided as [12, 34, 67] and [90].
🧠 Conceptual
intermediateUnderstanding the Role of Binary Search in Allocate Minimum Pages
Why do we use binary search in the Allocate Minimum Pages problem?
Attempts:
2 left
💡 Hint
Binary search is used on the range of possible answers, not on the array itself.
✗ Incorrect
Binary search helps to efficiently find the minimum maximum pages by checking feasibility of mid values.
🔧 Debug
advancedIdentify the Error in the Feasibility Check Function
What error will occur when running this feasibility check function for Allocate Minimum Pages problem?
DSA C
int isPossible(int arr[], int n, int m, int mid) { int students = 1, pages_sum = 0; for (int i = 0; i < n; i++) { if (arr[i] >= mid) return 0; if (pages_sum + arr[i] > mid) { students++; pages_sum = arr[i]; if (students > m) return 0; } else { pages_sum += arr[i]; } } return 1; }
Attempts:
2 left
💡 Hint
Check the condition that compares arr[i] and mid carefully.
✗ Incorrect
The condition should be arr[i] > mid, not >= mid, because if arr[i] equals mid, allocation is still possible.
❓ Predict Output
advancedOutput of Allocation with Single Student
What is the output of the minimum pages allocated when there is only one student for the books array?
DSA C
int arr[] = {10, 20, 30, 40}; int n = 4; int m = 1; int result = findPages(arr, n, m); printf("%d\n", result);
Attempts:
2 left
💡 Hint
If only one student is there, they must read all books.
✗ Incorrect
With one student, the minimum maximum pages is sum of all pages: 10+20+30+40=100.
🚀 Application
expertMinimum Pages Allocation for Large Input
Given 5 books with pages [15, 17, 20, 25, 30] and 3 students, what is the minimum maximum pages allocated to any student?
Attempts:
2 left
💡 Hint
Try dividing books into 3 contiguous parts minimizing the maximum sum.
✗ Incorrect
Optimal allocation is [15,17] (32), [20,25] (45), [30] (30) with max pages 45.
