Allocate Minimum Pages Binary Search on Answer in DSA C# - Time & Space Complexity
We want to find how long it takes to find the minimum maximum pages allocated using binary search on the answer space.
How does the number of steps grow as the number of books and pages increase?
Analyze the time complexity of the following code snippet.
bool IsPossible(int[] books, int students, int maxPages) {
int requiredStudents = 1, currentSum = 0;
foreach (int pages in books) {
if (pages > maxPages) return false;
if (currentSum + pages > maxPages) {
requiredStudents++;
currentSum = pages;
} else {
currentSum += pages;
}
}
return requiredStudents <= students;
}
int AllocateMinimumPages(int[] books, int students) {
int low = 0, high = 0;
foreach (int pages in books) high += pages;
int result = high;
while (low <= high) {
int mid = low + (high - low) / 2;
if (IsPossible(books, students, mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
This code tries to find the minimum maximum pages a student can get by using binary search on the possible answer range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
IsPossiblefunction scans all books once to check feasibility. - How many times: This check runs inside a binary search loop over the answer range, which repeats about
log(sum of pages)times.
As the number of books (n) grows, the scan inside IsPossible grows linearly. The binary search cuts the answer range roughly in half each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 books | About 10 * log(total pages) checks |
| 100 books | About 100 * log(total pages) checks |
| 1000 books | About 1000 * log(total pages) checks |
Pattern observation: Operations grow linearly with number of books and logarithmically with total pages.
Time Complexity: O(n * log S)
This means the time grows linearly with the number of books and logarithmically with the sum of all pages.
[X] Wrong: "The binary search runs in O(log n) where n is the number of books."
[OK] Correct: The binary search is on the range of possible page sums, not the number of books. The number of books affects the linear scan inside each check.
Understanding this time complexity helps you explain how binary search can be applied beyond sorted arrays, showing problem-solving skills that interviewers appreciate.
"What if we replaced the linear scan in IsPossible with a more complex check? How would that affect the time complexity?"
Practice
Allocate Minimum Pages problem when using binary search on the answer?Solution
Step 1: Understand the problem goal
The problem wants to split book pages among students so that the student with the most pages has as few pages as possible.Step 2: Identify the binary search target
Binary search is used to find the minimum possible maximum pages assigned to any student.Final Answer:
To minimize the maximum number of pages assigned to a student -> Option AQuick Check:
Goal = minimize max pages [OK]
- Thinking it maximizes total pages
- Confusing average with maximum
- Assuming random assignment
Solution
Step 1: Determine the minimum and maximum possible pages
The minimum max pages must be at least the largest single book (pages.Max()), and the maximum is the sum of all pages (pages.Sum()).Step 2: Set binary search range accordingly
Set low = pages.Max() and high = pages.Sum() to cover all possible max page values.Final Answer:
int low = pages.Max(), high = pages.Sum(); -> Option CQuick Check:
low = max page, high = sum pages [OK]
- Starting low at 0 (too low)
- Swapping low and high
- Using max as low and max as high
pages = [10, 20, 30, 40] and students = 2, what is the minimum maximum pages assigned after running the binary search solution?Solution
Step 1: Understand the problem with given input
We want to split [10,20,30,40] into 2 students minimizing the max pages assigned.Step 2: Check possible splits and their max pages
Possible splits: [10,20,30] & [40] max=60; [10,20] & [30,40] max=70; [10] & [20,30,40] max=90; [10,20,30,40] & [] invalid. The minimum max is 60 or 50? Actually, [10,20,30] & [40] max=60, but [10,20] & [30,40] max=70. Check if 50 is possible: [10,20] & [30,40] max=70 >50 no; [10,20,30] & [40] max=60 >50 no; So 50 is not possible. But 60 is possible. So minimum max pages is 60.Final Answer:
60 -> Option DQuick Check:
Minimum max pages = 60 [OK]
- Choosing max single book (40) as answer
- Choosing total sum (100) as answer
- Not checking all splits
bool IsPossible(int[] pages, int students, int mid) {
int count = 1, sum = 0;
foreach (int page in pages) {
if (sum + page > mid) {
count++;
sum = page;
} else {
sum += page;
}
}
return count <= students;
}
What is the bug in this code?Solution
Step 1: Analyze the feasibility check logic
The function checks if pages can be allocated so no student gets more than mid pages.Step 2: Identify missing check for single book pages
If any single book has pages > mid, allocation is impossible, but code does not check this, causing wrong results.Final Answer:
It does not handle the case when a single book has more pages than mid -> Option BQuick Check:
Check single book > mid to avoid wrong feasibility [OK]
- Ignoring single book size check
- Wrong comparison operator in return
- Resetting sum incorrectly
pages = [12, 34, 67, 90] and students = 2. Using binary search on answer, what is the minimum maximum pages that can be allocated? Also, explain why the answer is not the maximum single book pages.Solution
Step 1: Calculate total and max pages
Total pages = 12+34+67+90 = 203; max single book = 90.Step 2: Check possible splits for 2 students
Splits: [12,34,67] & [90] max=113; [12,34] & [67,90] max=157; [12] & [34,67,90] max=191. Minimum max pages is 113.Step 3: Explain why answer is not max single book
Because 90 alone is too small to cover all pages in 2 students without exceeding max pages, the minimum max pages must be higher than 90.Final Answer:
113, because splitting as [12,34,67] and [90] minimizes max pages -> Option AQuick Check:
Minimum max pages = 113 > max single book 90 [OK]
- Choosing max single book as answer
- Ignoring total pages sum
- Assuming equal division always works
Ready to prove it?
Pass the test and this pattern moves unknown
