0
0
DSA Goprogramming~30 mins

Allocate Minimum Pages Binary Search on Answer in DSA Go - Build from Scratch

Choose your learning style9 modes available
Allocate Minimum Pages using Binary Search on Answer
📖 Scenario: You are a librarian who needs to allocate books to students. Each book has a certain number of pages. You want to assign books to students so that the maximum number of pages assigned to any student is as small as possible.This helps keep the workload balanced among students.
🎯 Goal: Build a program that finds the minimum possible maximum pages assigned to a student by using binary search on the answer.
📋 What You'll Learn
Create a slice called books with these exact page counts: 100, 200, 300, 400
Create an integer variable called students and set it to 2
Write a function isPossible that takes books, students, and a max page limit, and returns true if allocation is possible
Use binary search between the max single book pages and sum of all pages to find the minimum max pages
Print the minimum max pages after allocation
💡 Why This Matters
🌍 Real World
This problem models distributing tasks or workloads evenly among workers to avoid overload.
💼 Career
Understanding binary search on answer is useful in optimization problems common in software engineering and coding interviews.
Progress0 / 4 steps
1
Create the books slice
Create a slice called books with these exact page counts: 100, 200, 300, 400
DSA Go
Hint

Use books := []int{100, 200, 300, 400} to create the slice.

2
Add the students variable
Create an integer variable called students and set it to 2
DSA Go
Hint

Use students := 2 to create the variable.

3
Write the isPossible function
Write a function called isPossible that takes books []int, students int, and maxPages int as parameters and returns bool. It should return true if it is possible to allocate books to students such that no student reads more than maxPages pages.
DSA Go
Hint

Check if any single book has more pages than maxPages. If yes, return false. Otherwise, count how many students are needed if we limit pages to maxPages.

4
Find and print the minimum maximum pages
Use binary search between the maximum pages in books and the sum of all pages in books to find the minimum maximum pages that can be allocated to students. Print the result using fmt.Println.
DSA Go
Hint

Use binary search between the max single book pages and total pages. Use isPossible to check feasibility. Print the final minimum max pages.