Allocate Minimum Pages

medium

You have a shelf of n books. The i-th book has arr[i] pages. You also have an integer k — the number of students.

Distribute all the books so that:

  • every student receives at least one book,
  • each student is given a contiguous block of books from the shelf,
  • no book is given to more than one student.

The busiest student is the one who receives the most pages in total. Among all valid distributions, find the one whose busiest student reads as few pages as possible — and return that number of pages.

If no valid distribution exists (more students than books), return -1.

Note: the answer always fits in a 32-bit integer.

Hints

You cannot try every way to cut the shelf — but could you verify a guess? If someone claims no student needs more than x pages, how fast can you check whether that is achievable?
For a fixed budget x, pack greedily left to right: fill the current student until the next book would exceed x, then start a new student. This uses the fewest students possible — if that count is at most k, the budget x is feasible.
Feasibility is monotone: no, no, no, yes, yes, yes. Binary search the smallest feasible budget between max(arr) and sum(arr) — each probe is one O(n) greedy pass.

Common doubts

Someone must take the thickest book, so no budget below max(arr) is achievable. Worse, the greedy counter assumes every single book fits within the budget — probing below max(arr) silently undercounts students and corrupts the search.
Sealing a student's pile earlier than forced never helps: the books you pushed to the next student only make later piles fuller. Stuffing each student to the brim before cutting is exchange-argument optimal, so if greedy needs more than k students, every allocation does.
The array is never sorted and never needs to be. The binary search runs over the range of candidate answers [max(arr), sum(arr)], which is monotone in feasibility — not over the array itself.
Just the number: the minimum possible value of the busiest student's page total. The cut positions themselves are not part of the answer.

Interview follow-ups

After the binary search converges to the answer x, run the greedy pass once more with budget x and record where each new student starts — those indices are the cut points of an optimal allocation.
That becomes multiway number partitioning, which is NP-hard. The greedy check stops being exact, so you would reach for backtracking with pruning, or approximation heuristics like LPT scheduling.
Yes — an interval-partition DP over prefix sums computes the same optimum in O(k · n^2) (improvable with monotonicity tricks), but for n up to 10^6 the binary-search-on-answer approach is the practical winner.

Fun facts

  • The array is never sorted here, yet binary search still applies — because the thing being searched is the space of candidate answers, not the array. That mental flip is the whole pattern.
  • The same guess-verify-halve skeleton solves Painter's Partition, Split Array Largest Sum, Koko Eating Bananas, and Aggressive Cows almost verbatim — master this one and you get four problems free.

Asked at

AmazonMicrosoftGoogleAdobeUber
Frequently Sometimes Occasionally
Example 1
Input: arr = [12, 34, 67, 90], k = 2
Output: 113
The possible splits are [12] | [34,67,90] (busiest reads 191), [12,34] | [67,90] (busiest reads 157), and [12,34,67] | [90] (busiest reads 113). The last split minimizes the busiest student's load, so the answer is 113.
Example 2
Input: arr = [15, 17, 20], k = 5
Output: -1
There are 5 students but only 3 books, so someone would get no book. Allocation is impossible.
Constraints

- 1 <= arr.size <= 10^6 - 1 <= arr[i] <= 10^4 - 1 <= k <= 10^4 - The answer always fits in a 32-bit integer

Solve this problem →