GeeksforGeeks Medium

Allocate Minimum Pages

Allocate Minimum Pages is a GFG problem (Medium). arr[i] is the number of pages in book i, and there are k students. Hand out every book so that:

  • each student gets at least one book;
  • each student's books form one contiguous run of the array;
  • no book goes to two students.

Return the smallest possible value for the largest page total any one student gets, or -1 if there are more students than books. With up to 10⁶ books, trying every way to cut the array is out of the question.

Constraints
  • 1 <= arr.size() <= 10⁶
  • 1 <= arr[i], k <= 10⁴
  • Each student gets a contiguous block of at least one book; return -1 if k > arr.size()
binary-searchgreedy
Open on GeeksforGeeks ↗
02

Intuition

Finding the best split directly is hard, but checking one guess is easy. Ask: if no student may read more than cap pages, how many students do we need? Walk the books left to right, give each book to the current student while it fits, and start a new student when it does not.

A bigger cap never needs more students. So the caps fall into two runs: small caps that need too many students, then caps that work. Binary search finds the first cap that works. It lies between max(arr), since the biggest book has to fit somewhere, and sum(arr), where one student reads everything.

How to spot this pattern

"Cut an array into at most k contiguous groups and make the largest group as small as possible" means: guess the largest group, count how many groups that guess forces, and binary search the guess.

03

Approach

Try it first

Before reading on, pick any page limit and work out how many students it needs. Then ask what happens to that count as the limit grows. Aim for O(n log S), where S is the total number of pages.

1

Rule out the impossible case

If k > len(arr), return -1. Every student needs at least one book, so there are not enough books to go round, and no cap can fix that.

2

Count the students a cap needs

Start with one student and a load of 0. For each book, if adding it would push the load past cap, start a new student whose load begins with this book; otherwise add it to the current load. Each student is filled as far as the cap allows, so the count is as small as it can be.

3

Set the search range

Set lo = max(arr) and hi = sum(arr). No cap below the biggest book can work, because books cannot be split, and the full sum always works because one student can take every book.

4

Binary search for the smallest cap that works

While lo < hi, try mid = (lo + hi) // 2. If it needs at most k students, set hi = mid, because mid works and a smaller cap might too. Otherwise set lo = mid + 1. When the two meet, lo is the smallest cap that works.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findPages(self, arr, k):
▶3 if k > len(arr):
▶4 return -1
▶5 
▶6 def students_for(cap):
▶7 count, load = 1, 0
▶8 for pages in arr:
▶9 if load + pages > cap:
▶10 count += 1
▶11 load = 0
▶12 load += pages
▶13 return count
▶14 
▶15 lo, hi = max(arr), sum(arr)
▶16 while lo < hi:
▶17 mid = (lo + hi) // 2
▶18 if students_for(mid) <= k:
▶19 hi = mid
▶20 else:
▶21 lo = mid + 1
▶22 return lo
lo 4hi 101234the cap is somewhere from 4 to 10
lo4biggest book
hi10all pages
students2
No cap below 4 can work, because that book cannot be split. A cap of 10 always works, because one student can read every book. The answer is the smallest cap in this range that needs at most 2 students.
lo 4hi 10cap 71 of 712341 page → student 1 starts
cap7
book1pages
student 11of 7
students1
Try a cap of 7, the middle of the range. The first book always opens student 1.
lo 4hi 10cap 73 of 712342 more pages fit → 3 of 7
cap7
book2pages
student 13of 7
students1
Student 1 has 1 page and this book adds 2, giving 3. That is still within 7, so the student keeps it: filling each student as far as the cap allows keeps the count of students as low as possible.
lo 4hi 10cap 76 of 712343 more pages fit → 6 of 7
cap7
book3pages
student 16of 7
students1
Student 1 has 3 pages and this book adds 3, giving 6. That is still within 7, so the student keeps it: filling each student as far as the cap allows keeps the count of students as low as possible.
lo 4hi 10cap 76 of 74 of 71234adding 4 would pass 7 → student 2 starts
cap7
book4pages
student 24of 7
students2
Adding 4 pages to student 1 would make 10, more than the cap of 7. So student 2 starts with this book.
lo 4hi 7cap 76 of 74 of 712342 students, we have 2 → cap 7 works, try lower
students needed2for cap 7
lo4
hi7
Cap 7 needs 2 students, which is at most 2, so it works. A smaller cap might work too, so the top of the range moves down: hi = 7.
lo 4hi 7cap 51 of 512341 page → student 1 starts
cap5
book1pages
student 11of 5
students1
Try a cap of 5, the middle of the range. The first book always opens student 1.
lo 4hi 7cap 53 of 512342 more pages fit → 3 of 5
cap5
book2pages
student 13of 5
students1
Student 1 has 1 page and this book adds 2, giving 3. That is still within 5, so the student keeps it: filling each student as far as the cap allows keeps the count of students as low as possible.
lo 4hi 7cap 53 of 53 of 51234adding 3 would pass 5 → student 2 starts
cap5
book3pages
student 23of 5
students2
Adding 3 pages to student 1 would make 6, more than the cap of 5. So student 2 starts with this book.
lo 4hi 7cap 53 of 53 of 54 of 51234adding 4 would pass 5 → student 3 starts
cap5
book4pages
student 34of 5
students3
Adding 4 pages to student 2 would make 7, more than the cap of 5. So student 3 starts with this book. That is already more than the 2 students we have.
lo 6hi 7cap 53 of 53 of 54 of 512343 students, only 2 → cap 5 too small
students needed3for cap 5
lo6
hi7
Cap 5 needs 3 students but there are only 2, so it is too small, and so is every cap below it. The bottom of the range moves up: lo = 6.
lo 6hi 7cap 61 of 612341 page → student 1 starts
cap6
book1pages
student 11of 6
students1
Try a cap of 6, the middle of the range. The first book always opens student 1.
lo 6hi 7cap 63 of 612342 more pages fit → 3 of 6
cap6
book2pages
student 13of 6
students1
Student 1 has 1 page and this book adds 2, giving 3. That is still within 6, so the student keeps it: filling each student as far as the cap allows keeps the count of students as low as possible.
lo 6hi 7cap 66 of 612343 more pages fit → 6 of 6
cap6
book3pages
student 16of 6
students1
Student 1 has 3 pages and this book adds 3, giving 6. That is still within 6, so the student keeps it: filling each student as far as the cap allows keeps the count of students as low as possible.
lo 6hi 7cap 66 of 64 of 61234adding 4 would pass 6 → student 2 starts
cap6
book4pages
student 24of 6
students2
Adding 4 pages to student 1 would make 10, more than the cap of 6. So student 2 starts with this book.
lo 6cap 66 of 64 of 612342 students, we have 2 → cap 6 works, try lower
students needed2for cap 6
lo6
hi6
Cap 6 needs 2 students, which is at most 2, so it works. A smaller cap might work too, so the top of the range moves down: hi = 6.
lo 6answer 66 pages4 pages1234range closed → smallest cap that works: 6
answer6
largest student6pages
lo and hi meet at 6. Every cap below 6 needed too many students, and 6 itself works, so it is the answer. With this cap the busiest student reads 6 pages.
05

Common pitfalls

Starting the search below the biggest book

✗ Wrong
lo, hi = 1, sum(arr)
✓ Right
lo, hi = max(arr), sum(arr)

With a cap smaller than one book, the count still gives that book its own student and reports the cap as fine. On [10, 100] with k = 2, the search returns 10, yet the second student reads 100 pages.

Asking for exactly k students

✗ Wrong
if students_for(mid) == k:
    hi = mid
✓ Right
if students_for(mid) <= k:
    hi = mid

A cap that needs fewer than k students still works: split any group with two or more books and the largest total does not grow. With == k the test is no longer one run of no followed by one run of yes, so the search breaks. On [1, 2, 3, 4] with k = 3 it returns 10 instead of 4.

Adding up pages in a 32-bit int

✗ Wrong
int hi = accumulate(arr.begin(), arr.end(), 0);
✓ Right
long long hi = accumulate(arr.begin(), arr.end(), 0LL);

Up to 10⁶ books of up to 10⁴ pages add up to 10¹⁰, well past the 2³¹ - 1 limit of an int. The sum wraps to a wrong value and the search runs on a broken range. The running load and mid need 64 bits for the same reason.

06

Edge cases

k equals the number of books

Each student gets exactly one book, so the answer is the biggest book. Every cap above it needs at most k students, so the search ends at lo = max(arr).

k is 1

One student reads everything. Every cap below sum(arr) needs a second student, so the search ends at hi = sum(arr).

07

Complexity

Time
O(n log S)
Space
O(1)
n is the number of books and S is sum(arr) - max(arr), the width of the search range. Each guess halves the range and costs one O(n) pass to count students. Only a few counters are stored.
08

Allocate Minimum Pages FAQ

Why does the greedy count give the fewest students for a cap?

Compare it with any other valid split. Greedy's first student stops only when the next book would not fit, so it ends at the same book as the other split's first student or later. Repeat the argument for each student: greedy's i-th student always ends at or after the other split's i-th student. So greedy runs out of books no later, using the same number of students or fewer.

How is this different from Split Array Largest Sum?

It is the same task under another name. Split Array Largest Sum (LeetCode 410) cuts an array into k non-empty parts and minimises the largest part sum; its constraints promise k is at most the array length, so it never returns -1. The Painter's Partition problem and Capacity to Ship Packages Within D Days (LeetCode 1011) use the same guess-and-count search with painters or days in place of students.

Can I sort the books first?

No. Each student must get a contiguous run of the original order, and sorting changes which books sit next to each other. For [10, 40, 20, 30] with k = 2 the answer is 50 ([10, 40] and [20, 30]); the sorted array [10, 20, 30, 40] gives 60.