Skip to content
MediumBinary SearchAI interview only

Book Allocation Minimum Pages

Asked atamazongooglemicrosoftadobegoldman-sachs

01 · Problem

You are given an array pages where pages[i] is the number of pages in the i-th book, and an integer k, the number of students.

Distribute all books among the k students so that:

  • every student receives at least one book,
  • each student receives a contiguous block of books (in the given order), and
  • every book goes to exactly one student.

Among all valid distributions, minimise the largest number of pages assigned to any single student, and return that minimum. If a valid distribution is impossible (more students than books), return -1.

02 · Examples

Example 01
Input
pages = [12,34,67,90], k = 2
Output
113

Give books [12, 34, 67] (113 pages) to the first student and [90] to the second. Every other split has a larger maximum.

Example 02
Input
pages = [15,17,20], k = 2
Output
32

Split into [15, 17] (32 pages) and [20]. The alternative [15] and [17, 20] has a maximum of 37.

Example 03
Input
pages = [5,10], k = 3
Output
-1

There are only 2 books but 3 students, and each student must receive at least one book.

03 · Constraints

  • 011 <= pages.length <= 105
  • 021 <= pages[i] <= 104
  • 031 <= k <= 105

04 · Optimal complexity

Time
O(n log(sum(pages)))
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.