Allocate Minimum Pages Binary Search on Answer in DSA Go - Time & Space Complexity
We want to find how fast the binary search approach solves the problem of allocating minimum pages.
Specifically, how the number of steps grows as the number of books or pages increases.
Analyze the time complexity of the following code snippet.
func isPossible(books []int, students, mid int) bool {
count := 1
sum := 0
for _, pages := range books {
if pages > mid {
return false
}
if sum+pages > mid {
count++
sum = pages
if count > students {
return false
}
} else {
sum += pages
}
}
return true
}
func allocateMinimumPages(books []int, students int) int {
start, end := 0, 0
for _, pages := range books {
if pages > start {
start = pages
}
end += pages
}
result := end
for start <= end {
mid := (start + end) / 2
if isPossible(books, students, mid) {
result = mid
end = mid - 1
} else {
start = mid + 1
}
}
return result
}
This code tries to find the smallest maximum number of pages assigned to any student by using binary search on the answer range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The binary search loop that runs while narrowing the page range.
- How many times: The binary search runs about log of the sum of pages times.
- Inside each binary search step: The
isPossiblefunction loops through all books once (n times). - Dominant operation: The repeated full traversal of the books array inside each binary search step.
As the number of books (n) grows, each check scans all books once.
The binary search cuts the search space of possible page sums roughly in half each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 books | About 10 * log(sum of pages) checks |
| 100 books | About 100 * log(sum of pages) checks |
| 1000 books | About 1000 * log(sum of pages) checks |
Pattern observation: The total steps grow linearly with the number of books and logarithmically with the total pages.
Time Complexity: O(n * log m)
This means the time grows linearly with the number of books and logarithmically with the sum of all pages.
[X] Wrong: "The binary search alone makes this O(log n) time."
[OK] Correct: Each binary search step requires scanning all books to check feasibility, so the linear scan dominates.
Understanding this time complexity helps you explain how binary search can be applied beyond simple arrays, showing problem-solving skills.
"What if we used a different feasibility check that skips some books? How would that affect the time complexity?"
Practice
Allocate Minimum Pages problem using binary search on answer?Solution
Step 1: Understand the problem objective
The problem asks to allocate books to students such that the maximum pages assigned to any student is minimized.Step 2: Identify the binary search approach
We use binary search on the range of possible maximum pages to find the minimum feasible maximum.Final Answer:
To minimize the maximum number of pages assigned to a student -> Option BQuick Check:
Goal = minimize max pages [OK]
- Confusing minimizing max pages with maximizing total pages
- Thinking the problem counts students instead of pages
- Ignoring the binary search on answer approach
Solution
Step 1: Determine minimum and maximum boundaries
The minimum possible max pages is the largest single book pages, because no book can be split.Step 2: Set high boundary as total pages
The maximum possible max pages is the sum of all pages if one student reads all books.Final Answer:
low := max pages in a single book; high := sum of all pages -> Option CQuick Check:
Boundaries = max single book to total pages [OK]
- Setting low to 0 which is invalid
- Setting high less than sum of pages
- Confusing number of students with page boundaries
Solution
Step 1: Check possible max pages allocations
Try max pages = 50: allocation could be [10,20,30] and [40], but first student has 60 pages, which is >50, so invalid.Step 2: Try max pages = 60
Allocation: [10,20,30] = 60 pages, [40] = 40 pages. Both <= 60, valid allocation.Final Answer:
60 -> Option DQuick Check:
Minimum max pages = 60 [OK]
- Choosing max single book pages as answer without checking allocation
- Assuming equal split always works
- Not verifying allocation feasibility
func isPossible(books []int, students int, mid int) bool {
count := 1
sum := 0
for _, pages := range books {
if pages > mid {
return false
}
sum += pages
if sum > mid {
count++
sum = pages
}
}
return count <= students
}Solution
Step 1: Analyze sum reset logic
When sum exceeds mid, count increments and sum resets to current pages, which is correct to start new allocation.Step 2: Check return condition
Returns true if count of students needed is less than or equal to available students, which is correct.Final Answer:
No bug, code is correct -> Option AQuick Check:
Sum reset and count logic correct [OK]
- Resetting sum to 0 instead of current pages
- Returning true when count exceeds students
- Incrementing count too early
Solution
Step 1: Calculate sum and max single book pages
Sum = 12+34+67+90 = 203; max single book = 90; low=90, high=203.Step 2: Check allocation with mid = (90+203)/2 = 146
Try allocation: [12,34,67] = 113 pages, [90] = 90 pages; max = 113 <= 146, feasible.Step 3: Try smaller mid to find minimum max pages
Try mid=113: allocation same as above, feasible. Try mid=112: allocation fails.Step 4: Explain why [12,34] and [67,90] is not optimal
Allocation sums: 46 and 157; max is 157 which is greater than minimum max pages 113.Final Answer:
113; Because max pages is 157 which is more than minimum possible -> Option AQuick Check:
Minimum max pages = 113; given allocation max = 157 [OK]
- Assuming largest single book pages is answer
- Not verifying allocation sums
- Confusing median with allocation max pages
Ready to prove it?
Pass the test and this pattern moves unknown
