Challenge - 5 Problems
Minimum Pages Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Minimum Pages Allocation for Given Books and Students
What is the output of the following Go code that uses binary search to allocate minimum pages to 2 students for 4 books?
DSA Go
package main import "fmt" func isPossible(books []int, students int, 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 } func main() { books := []int{10, 20, 30, 40} students := 2 fmt.Println(allocateMinimumPages(books, students)) }
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 to 2 students for books with pages [10,20,30,40] is 60. The optimal division is [10,20,30] totaling 60 and [40] totaling 40.
🧠 Conceptual
intermediate1:30remaining
Understanding the Role of Binary Search in Minimum Pages Allocation
Why do we use binary search on the answer space instead of the input array when solving the Allocate Minimum Pages problem?
Attempts:
2 left
💡 Hint
Think about the range of possible answers and how binary search can narrow it down.
✗ Incorrect
The problem's answer is a number between the largest single book pages and the total pages. We binary search this range to find the minimal maximum pages allocation that is feasible.
🔧 Debug
advanced2:00remaining
Identify the Bug in the Allocation Feasibility Check
What error will occur when running this feasibility check function for allocating pages?
DSA Go
func isPossible(books []int, students int, 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 }
Attempts:
2 left
💡 Hint
Check the condition that compares pages with mid carefully.
✗ Incorrect
The condition 'if pages >= mid' returns false even when pages == mid, but it should allow pages equal to mid. This causes incorrect feasibility results.
❓ Predict Output
advanced2:00remaining
Output of Allocation with 3 Students and 5 Books
What is the output of the following Go code that allocates minimum pages to 3 students for 5 books?
DSA Go
package main import "fmt" func isPossible(books []int, students int, 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 } func main() { books := []int{12, 34, 67, 90, 45} students := 3 fmt.Println(allocateMinimumPages(books, students)) }
Attempts:
2 left
💡 Hint
Try dividing books into 3 parts minimizing the maximum pages assigned.
✗ Incorrect
The optimal allocation is [12,34,67] totaling 113, [90] totaling 90, [45] totaling 45 with max pages 113.
🚀 Application
expert3:00remaining
Minimum Pages Allocation for Large Input with Constraints
Given 7 books with pages [15, 17, 20, 25, 30, 35, 40] and 4 students, what is the minimum maximum pages allocated to any student?
Attempts:
2 left
💡 Hint
Use binary search between max single book pages and sum of all pages to find minimal max allocation.
✗ Incorrect
The minimal maximum pages is 55 by allocating books as [15,17,20], [25,30], [35], [40] or similar grouping.