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 distribute the books so that the student who gets the most pages has as few pages as possible. Books must be allocated in order and each student must get at least one book.
🎯 Goal: Build a program that finds the minimum possible maximum number of pages assigned to a student using binary search on the answer.
📋 What You'll Learn
Create a vector called books with these exact page counts: 12, 34, 67, 90
Create an integer variable called students and set it to 2
Write a function called isPossible that takes books, students, and a maxPages integer, and returns true if allocation is possible without exceeding maxPages per student
Use binary search between the maximum single book pages and the sum of all pages to find the minimum maximum pages
Print the minimum maximum pages after allocation
💡 Why This Matters
🌍 Real World
This problem models distributing workloads or resources evenly, such as assigning books to students or tasks to workers.
💼 Career
Understanding binary search on answer is useful in software engineering roles that require optimization and efficient resource allocation.
Progress0 / 4 steps
1
Create the books vector
Create a vector of integers called books with these exact values: 12, 34, 67, 90
DSA C++
Hint
Use std::vector<int> and initialize it with the given numbers inside curly braces.
2
Add the students variable
Add an integer variable called students and set it to 2 below the books vector
DSA C++
Hint
Declare int students = 2; after the books vector.
3
Write the isPossible function
Write a function called isPossible that takes const std::vector<int>& books, int students, and int maxPages as parameters and returns true if it is possible to allocate books to students so that no student reads more than maxPages pages. Books must be allocated in order and each student must get at least one book.
DSA C++
Hint
Use a loop to sum pages and count students needed. Return false if any book has more pages than maxPages or if students needed exceed given students.
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 std::cout.
DSA C++
Hint
Calculate the max single book pages and total pages. Use binary search between these to find minimum max pages. Print the final answer.
Practice
(1/5)
1. What is the main goal of the Allocate Minimum Pages problem using binary search on the answer?
easy
A. To assign pages randomly to students
B. To maximize the total number of pages assigned
C. To find the average number of pages per student
D. To minimize the maximum number of pages assigned to a student
Solution
Step 1: Understand the problem objective
The problem wants to divide book pages among students so that the student with the most pages has as few pages as possible.
Step 2: Recognize the use of binary search on answer
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 D
Quick Check:
Goal = minimize max pages per student [OK]
Hint: Focus on minimizing the largest pages assigned [OK]
Common Mistakes:
Thinking it maximizes total pages
Confusing average with maximum
Ignoring the binary search approach
2. Which of the following is the correct way to initialize the binary search boundaries for the Allocate Minimum Pages problem in C++?
easy
A. int low = max pages in a single book, high = sum of all pages;
B. int low = 0, high = sum of all pages;
C. int low = 0, high = max pages in a single book;
D. int low = sum of all pages, high = max pages in a single book;
Solution
Step 1: Determine the minimum and maximum boundaries
The minimum possible max pages is the largest single book (no student can get less than that), and the maximum is the sum of all pages (one student gets all books).
Step 2: Set binary search boundaries accordingly
So, low = max pages in a single book, high = sum of all pages.
Final Answer:
int low = max pages in a single book, high = sum of all pages; -> Option A
Quick Check:
low = max single book, high = total pages [OK]
Hint: Low = max single book pages, High = sum of all pages [OK]
Common Mistakes:
Setting low to 0 which is invalid
Swapping low and high values
Using average pages as boundary
3. What will be the output of the following C++ code snippet for the Allocate Minimum Pages problem?
int pages[] = {10, 20, 30, 40};
int n = 4, students = 2;
// Function to check feasibility
bool isPossible(int mid) {
int studentCount = 1, pageSum = 0;
for (int i = 0; i < n; i++) {
if (pages[i] > mid) return false;
if (pageSum + pages[i] > mid) {
studentCount++;
pageSum = pages[i];
if (studentCount > students) return false;
} else {
pageSum += pages[i];
}
}
return true;
}
int low = 40, high = 100, result = high;
while (low <= high) {
int mid = (low + high) / 2;
if (isPossible(mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
std::cout << result << std::endl;
medium
A. 50
B. 70
C. 60
D. 40
Solution
Step 1: Understand the feasibility check
The function checks if pages can be allocated so no student gets more than mid pages.
Step 2: Binary search to find minimum max pages
Try mid values between 40 and 100. The minimum feasible max pages is 60 (assign {10,20,30} to student 1 and {40} to student 2).
Final Answer:
60 -> Option C
Quick Check:
Minimum max pages = 60 [OK]
Hint: Check feasibility by simulating allocation [OK]
Common Mistakes:
Not updating student count correctly
Choosing mid incorrectly
Ignoring single book page size
4. Identify the bug in the following C++ code snippet for the Allocate Minimum Pages problem:
bool isPossible(int mid, int pages[], int n, int students) {
int studentCount = 1, pageSum = 0;
for (int i = 0; i < n; i++) {
if (pageSum + pages[i] <= mid) {
pageSum += pages[i];
} else {
studentCount++;
pageSum = pages[i];
}
}
return studentCount <= students;
}
medium
A. Missing check if a single book has pages more than mid
B. Incorrect increment of studentCount
C. Page sum should reset to zero instead of pages[i]
D. Loop should run from i = 1 instead of 0
Solution
Step 1: Analyze the feasibility function
The function tries to allocate books without exceeding mid pages per student.
Step 2: Identify missing validation
It does not check if any single book has pages more than mid, which makes allocation impossible.
Final Answer:
Missing check if a single book has pages more than mid -> Option A
Quick Check:
Check single book pages <= mid [OK]
Hint: Always check single book pages against mid [OK]
Common Mistakes:
Ignoring single book size check
Resetting pageSum incorrectly
Wrong loop start index
5. Given the books with pages {12, 34, 67, 90} and 2 students, what is the minimum maximum number of pages a student can get when using binary search on answer to allocate books optimally?
hard
A. 90
B. 113
C. 101
D. 67
Solution
Step 1: Set binary search boundaries
Low = max single book = 90, High = sum = 12+34+67+90=203.
Step 2: Use binary search to find minimum max pages
Check mid values. The minimum feasible max pages is 113 (assign {12,34,67}=113 to one student and {90} to the other).
Final Answer:
113 -> Option B
Quick Check:
Minimum max pages = 113 [OK]
Hint: Binary search between max single book and total pages [OK]
Common Mistakes:
Choosing max single book as answer
Choosing sum of pages as answer
Not testing feasibility properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.