Allocate Minimum Pages Binary Search on Answer in DSA Typescript - Time & Space Complexity
We want to find how fast the binary search approach works to allocate minimum pages among students.
How does the number of operations grow as the number of books or pages increases?
Analyze the time complexity of the following code snippet.
function isPossible(books: number[], students: number, maxPages: number): boolean {
let requiredStudents = 1;
let currentSum = 0;
for (const pages of books) {
if (pages > maxPages) return false;
if (currentSum + pages > maxPages) {
requiredStudents++;
currentSum = pages;
if (requiredStudents > students) return false;
} else {
currentSum += pages;
}
}
return true;
}
function allocateMinimumPages(books: number[], students: number): number {
let start = Math.max(...books);
let end = books.reduce((a, b) => a + b, 0);
let result = end;
while (start <= end) {
const mid = Math.floor((start + end) / 2);
if (isPossible(books, students, mid)) {
result = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return result;
}
This code uses binary search on the answer range to find the minimum maximum pages allocated to any student.
Look at the loops and checks that repeat.
- Primary operation: The for-loop inside
isPossiblethat checks if allocation is possible. - How many times: This loop runs once for each book, so
ntimes per check. - Binary search loop: The while loop in
allocateMinimumPagesruns aboutlog(sum of pages)times.
Each binary search step checks all books once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 books | 10 x log(total pages) |
| 100 books | 100 x log(total pages) |
| 1000 books | 1000 x log(total pages) |
As books increase, operations grow linearly with n, multiplied by the logarithm of total pages.
Time Complexity: O(n x log m)
This means the time grows linearly with the number of books and logarithmically with the sum of all pages.
[X] Wrong: "The binary search alone makes this O(log n) time."
[OK] Correct: Each binary search step runs a full check over all books, so the total time depends on both the number of books and the binary search steps.
Understanding this time complexity helps you explain how binary search can be used on answers, a common pattern in allocation and partition problems.
What if we used a linear search instead of binary search on the answer range? How would the time complexity change?
Practice
Allocate Minimum Pages problem using binary search on answer?Solution
Step 1: Understand the problem objective
The problem asks to allocate books to students such that the maximum pages assigned to any student is minimized.Step 2: Identify the binary search approach
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 DQuick Check:
Goal = minimize max pages [OK]
- Trying to maximize pages instead of minimizing
- Ignoring the maximum pages constraint
- Confusing total pages with max pages per student
pages?Solution
Step 1: Determine low and high boundaries
Low is minimum possible max pages (0 or max single book pages), high is sum of all pages (max total pages).Step 2: Check correct syntax for sum
Usingreducesums all pages correctly for high boundary.Final Answer:
let low = 0; let high = pages.reduce((a, b) => a + b, 0); -> Option CQuick Check:
low = 0, high = sum pages [OK]
- Setting low as pages.length instead of page count
- Using min pages as high boundary
- Incorrect sum calculation syntax
function isPossible(pages: number[], students: number, maxPages: number): boolean {
let requiredStudents = 1;
let currentSum = 0;
for (let page of pages) {
if (page > maxPages) return false;
if (currentSum + page > maxPages) {
requiredStudents++;
currentSum = page;
} else {
currentSum += page;
}
}
return requiredStudents <= students;
}What is the output of isPossible([10, 20, 30, 40], 2, 50)?Solution
Step 1: Trace allocation with maxPages = 50
Assign pages: 10+20=30 (still <=50), add 30 would exceed 50, so new student starts with 30, then 40 exceeds 50 if added, so new student starts with 40.Step 2: Count required students
We used 3 students but only 2 allowed, so requiredStudents=3 > 2 means return false. But check carefully: 10+20=30, next 30 alone fits in second student, 40 alone needs third student.Final Answer:
false -> Option AQuick Check:
Required students = 3 > 2, so false [OK]
- Ignoring that a single book can exceed maxPages
- Miscounting students when sum exceeds maxPages
- Assuming sum of all pages fits in one student
function findMinPages(pages: number[], students: number): number {
let low = Math.min(...pages);
let high = pages.reduce((a, b) => a + b, 0);
let result = high;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (isPossible(pages, students, mid)) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}Solution
Step 1: Analyze low boundary initialization
Low should be the maximum single book pages, because no student can get less than the largest book.Step 2: Identify impact of wrong low
Starting low at minimum page can cause incorrect feasibility checks and longer search.Final Answer:
Low should start from max page, not min page -> Option BQuick Check:
Low = max page [OK]
- Setting low to min page instead of max page
- Confusing high boundary with low
- Not updating result inside loop
[12, 34, 67, 90] and 3 students, what is the minimum maximum pages allocated to any student using binary search on answer?Solution
Step 1: Set search boundaries
Low = max pages = 90, High = sum pages = 12+34+67+90=203.Step 2: Binary search feasibility checks
Mid = (90+203)//2=146. Check if possible with maxPages=146: allocate [12+34+67=113] to student1, 90 to student2, only 2 students needed <=3, possible. So high=145.Step 3: Narrow down to minimum max pages
Continuing: mid=103 possible [12+34=46,67,90] 3 students. mid=96 possible. mid=92 possible. mid=90: [12+34=46], [67], [90] all <=90, 3 students <=3 feasible, high=89. Loop ends with result=90.Step 4: Re-evaluate minimum max pages
Check if 113 is feasible with 3 students: [12+34+67=113], [90] 2 students needed <=3, so 113 is feasible but not minimum.Step 5: Check if 90 is feasible
Allocations: [12+34=46], [67], [90] 3 students needed <=3, feasible.Final Answer:
90 -> Option AQuick Check:
Minimum max pages = 90 [OK]
- Misallocating at max=90 thinking 4 students needed
- Miscounting students during allocation
- Confusing with 2 students (113)
Ready to prove it?
Pass the test and this pattern moves unknown
