DSA AnimatorDSA animations
Allocate Minimum Pages GFG Hard Binary Search on Answer
Problem

Given an array pages[] of book page counts and m students, allocate books to students such that: (1) each student gets a contiguous subset of books, (2) each book is allocated to exactly one student. Minimize the maximum pages allocated to any student.

Example 1
Input: pages = [12,34,67,90], m = 2
Output: 113
Explanation: Student 1: [12,34,67] = 113 pages, Student 2: [90] = 90 pages. Max = 113.
Example 2
Input: pages = [10,20,30,40], m = 2
Output: 60
Explanation: Student 1: [10,20,30] = 60 pages, Student 2: [40] = 40 pages. Max = 60.
Example 3
Input: pages = [15,17,20], m = 2
Output: 32
Explanation: Student 1: [15,17] = 32 pages, Student 2: [20] = 20 pages. Max = 32.
Constraints: 1 ≤ m ≤ pages.length ≤ 10⁵  |  1 ≤ pages[i] ≤ 10⁶
Try Examples
Custom:
Search Range — Max Pages per Student
lo
—
mid
—
hi
—
lo = max(pages) hi = sum(pages)
—
Book Assignment — Greedy Simulation at candidate mid
Waiting…
Variables
lo
—
hi
—
mid
—
students
—
ans
—
Step Logic
Press ▶ Play or Next Step to begin.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init: lo = max(pages), hi = sum(pages), ans = hi
2
While lo ≤ hi: compute mid = lo + (hi−lo)/2
3
Greedy: count students needed for limit mid
4
If students ≤ m: ans=mid, hi=mid−1 (try smaller)
5
Else: lo=mid+1 (limit too tight)
6
Return ans — minimum possible maximum pages
Time
O(n log(sum))
Space
O(1)
Why Binary Search on Answer Works

The feasibility function is monotone: if we can allocate with max limit X, we can also do it with any larger limit. So there is a threshold — everything below it is infeasible, everything above is feasible. Binary search finds the minimum feasible value in O(log(sum)) iterations, each costing O(n) for greedy simulation.

How to solve Allocate Minimum Number of Pages

GFG · Hard · Binary Search · Pattern: Binary Search on Answer

Why the obvious approach is slow

Trying every way to split the books is exponential. But if a page limit works, every bigger limit works too.

The key idea

Binary search the page limit between the largest book and the total. For a limit, give books to a student until the next one would exceed it, then move to the next student. If the students needed are at most the given number, try smaller.

Remember

This is the same problem as split array largest sum. Binary search the limit, and count students greedily.

Run the animation above on your own input, or watch the 1:25 video explanation of Allocate Minimum Number of Pages with a dry run and Java code.

More Binary Search problems