Allocate Minimum Pages Binary Search on Answer in DSA Rust - Time & Space Complexity
We want to find how fast the binary search approach works to allocate minimum pages among students.
Specifically, how the number of operations grows as the number of books or pages increases.
Analyze the time complexity of the following code snippet.
fn is_possible(books: &[i32], students: i32, max_pages: i32) -> bool {
let mut required_students = 1;
let mut current_sum = 0;
for &pages in books {
if pages > max_pages {
return false;
}
if current_sum + pages > max_pages {
required_students += 1;
current_sum = pages;
if required_students > students {
return false;
}
} else {
current_sum += pages;
}
}
true
}
fn allocate_min_pages(books: &[i32], students: i32) -> i32 {
let mut low = *books.iter().max().unwrap();
let mut high: i32 = books.iter().sum();
let mut result = high;
while low <= high {
let mid = low + (high - low) / 2;
if is_possible(books, students, mid) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
result
}
This code tries to find the smallest maximum number of pages assigned to any student by using binary search on the answer range.
- Primary operation: The binary search loop runs over the range of possible page sums.
- How many times: Binary search runs about log of the sum of pages times.
- Inside each binary search step: The
is_possiblefunction traverses all books once (linear scan). - Dominant operation: The linear scan inside
is_possiblerepeated for each binary search step.
As the number of books (n) increases, the linear scan inside is_possible grows linearly.
The binary search runs about log of the total pages range, which depends on the sum of pages.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 books | ~10 * log(sum of pages) |
| 100 books | ~100 * log(sum of pages) |
| 1000 books | ~1000 * log(sum of pages) |
Pattern observation: Operations grow linearly with number of books and logarithmically with the range of pages.
Time Complexity: O(n * log m)
This means the time grows linearly with the number of books and logarithmically with the total pages range.
[X] Wrong: "The binary search alone makes this O(log n) time."
[OK] Correct: Each binary search step requires scanning all books, so the linear scan dominates and multiplies the log factor.
Understanding this time complexity helps you explain how binary search can be applied on answers, a common pattern in coding problems.
It shows how combining binary search with linear checks balances speed and correctness.
What if we replaced the linear scan in is_possible with a more complex check? How would that affect the overall time complexity?
Practice
Allocate Minimum Pages problem using binary search on the answer?Solution
Step 1: Understand the problem objective
The problem aims to distribute book pages among students such that the maximum pages assigned to any student is as small as possible.Step 2: Identify the goal of binary search on answer
Binary search is used to find this minimum possible maximum pages value efficiently.Final Answer:
To minimize the maximum number of pages assigned to a student -> Option AQuick Check:
Goal = minimize max pages [OK]
- Thinking the goal is to maximize pages
- Confusing average pages with maximum pages
- Assigning pages randomly without constraints
Solution
Step 1: Analyze parameter types for pages and students
Pages should be a slice reference&[u32]for read-only access, and students count is au32as a positive integer.Step 2: Check return type for minimum maximum pages
The function returns au32representing the minimum maximum pages allocated.Final Answer:
fn allocate_pages(pages: &[u32], students: u32) -> u32 -> Option BQuick Check:
Signature matches input slice and u32 output [OK]
- Using Vec instead of slice for input when not modifying
- Mixing usize and u32 inconsistently
- Returning usize instead of u32 for pages
[10, 20, 30, 40] and 2 students, what is the minimum maximum pages allocated after running the binary search algorithm?Solution
Step 1: Calculate search range for binary search
Minimum pages = max page = 40, maximum pages = sum = 100.Step 2: Apply binary search to find minimum max pages
Check mid = 70: possible to allocate? Yes (10+20+30=60 <=70, 40 alone). Try lower mid = 60: possible? Yes (10+20+30=60, 40 alone). Try mid = 50: possible? No (10+20=30, next 30 alone, 40 alone means 3 students needed). So minimum max pages = 60.Final Answer:
60 -> Option AQuick Check:
Minimum max pages = 60 [OK]
- Choosing sum of all pages as answer
- Ignoring max single page as lower bound
- Miscounting number of students needed
fn is_possible(pages: &[u32], students: u32, max_pages: u32) -> bool {
let mut count = 1;
let mut total = 0;
for &page in pages {
total += page;
if total > max_pages {
count += 1;
total = page;
}
}
count <= students
}Solution
Step 1: Understand feasibility check logic
The function sums pages until exceeding max_pages, then assigns to next student.Step 2: Identify missing check for single page size
If any single page is larger than max_pages, allocation is impossible, but code does not check this explicitly.Final Answer:
It does not check if a single page is greater than max_pages -> Option CQuick Check:
Check single page > max_pages missing [OK]
- Ignoring single page size check
- Resetting total before incrementing count
- Using wrong variable types causing overflow
[12, 34, 67, 90] and 2 students. Using binary search on answer, what is the minimum maximum pages allocation? Also, explain why the naive approach of assigning equal pages to each student might fail.Solution
Step 1: Calculate binary search range and feasibility
Max single page = 90, sum = 203. Check mid = 113: allocate (12+34+67=113) to first student, 90 to second student. Possible. Try lower mid = 101: (12+34=46, next 67 alone, 90 alone) needs 3 students, not possible. So minimum max pages = 113.Step 2: Explain naive approach failure
Naive approach divides total pages (203) by students (2) = 101.5, but cannot split pages arbitrarily or ignore max single page size, so it fails to find feasible allocation.Final Answer:
113; Naive approach ignores page order and max page size -> Option DQuick Check:
Minimum max pages = 113; naive fails due to constraints [OK]
- Assuming equal division always works
- Ignoring max single page size
- Not testing feasibility with actual page grouping
Ready to prove it?
Pass the test and this pattern moves unknown
