← Back to DSA Animator
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.