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 Rust function that finds the minimum number of pages allocated to a student when books are divided optimally among students?
DSA Rust
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 } fn main() { let books = vec![12, 34, 67, 90]; let students = 2; let result = allocate_min_pages(&books, students); println!("{}", result); }
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the maximum pages a student can get.
✗ Incorrect
The minimum maximum pages allocated when dividing books [12, 34, 67, 90] among 2 students is 113. One optimal division is [12, 34, 67] and [90], where the maximum pages assigned to a student is 113.
🧠 Conceptual
intermediateUnderstanding the role of 'is_possible' function in Allocate Minimum Pages
What does the 'is_possible' function check in the Allocate Minimum Pages problem?
Attempts:
2 left
💡 Hint
Focus on the condition checked inside the loop for each book.
✗ Incorrect
The 'is_possible' function checks if it is possible to allocate books to the given number of students such that no student reads more than max_pages pages.
🔧 Debug
advancedIdentify the error in this Rust code snippet for Allocate Minimum Pages
What error will this Rust code produce when compiled?
DSA Rust
fn allocate_min_pages(books: &[i32], students: i32) -> i32 {
let mut low = 0;
let mut high = books.iter().sum();
let mut result = high;
while low < high {
let mid = (low + high) / 2;
if is_possible(books, students, mid) {
result = mid;
high = mid;
} else {
low = mid + 1;
}
}
result
}
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
}Attempts:
2 left
💡 Hint
Check the binary search loop condition and how high is updated.
✗ Incorrect
The loop condition 'while low < high' with 'high = mid;' can cause infinite loop when low == mid, because high does not decrease below mid.
🚀 Application
advancedApplying Allocate Minimum Pages to a new scenario
Given books with pages [10, 20, 30, 40, 50] and 3 students, what is the minimum maximum pages allocated to a student?
Attempts:
2 left
💡 Hint
Try dividing books into 3 parts minimizing the maximum sum.
✗ Incorrect
One optimal division is [10, 20, 30], [40], [50]. The maximum pages assigned is 70.
🧠 Conceptual
expertWhy binary search is used in Allocate Minimum Pages problem?
Why is binary search an efficient approach to solve the Allocate Minimum Pages problem?
Attempts:
2 left
💡 Hint
Think about how the search space is defined and how feasibility is tested.
✗ Incorrect
Binary search efficiently narrows down the minimum maximum pages by checking feasibility in a numeric range without trying all allocations.
