VOOZH about

URL: https://www.geeksforgeeks.org/dsa/allocate-minimum-number-pages/

⇱ Allocate Minimum Pages - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Allocate Minimum Pages

Last Updated : 12 Aug, 2025

Given an array arr[], where arr[i] represents the number of pages in the i-th book, and an integer k denoting the total number of students, allocate all books to the students such that:

  • Each student gets at least one book.
  • Books are allocated in a contiguous sequence.
  • The maximum number of pages assigned to any student is minimized.

If it is not possible to allocate all books among k students under these conditions, return -1.

Examples:

Input: arr[] = [12, 34, 67, 90], k = 2
Output: 113
Explanation: Books can be distributed in following ways:

  • [12] and [34, 67, 90] - The maximum pages assigned to a student is 34 + 67 + 90 = 191.
  • [12, 34] and [67, 90] - The maximum pages assigned to a student is 67 + 90 = 157.
  • [12, 34, 67] and [90] - The maximum pages assigned to a student is 12 + 34 + 67 = 113.

The third combination has the minimum pages assigned to a student which is 113.

Input: arr[] = [15, 17, 20], k = 5
Output: -1
Explanation: Since there are more students than total books, it's impossible to allocate a book to each student.

Input: arr[] = [22, 23, 67], k = 1
Output: 112
Explanation: Since there is only 1 student, all books are assigned to that student. So, maximum pages assigned to a student is 22 + 23 + 67 = 112.

[Naive Approach] By Iterating Over All Possible Page Limits

The approach is to search for the minimum possible value of the maximum number of pages that can be assigned to any student.

  • The lowest possible page limit is the maximum number of pages in a single book, since that book must be assigned to some student.
  • The highest possible page limit is the total number of pages across all books, which would happen if one student gets all the books.

To check if a given page limit can work, we simulate the process of assigning books to students:
We start with the first student and keep assigning books one by one until adding the next book would exceed the current page limit. At that point, we assign books to the next student, and continue this process.

As soon as we find the smallest page limit that allows us to allocate all books to exactly k students, we return it.


Output
113

Time Complexity: O(n × (sum(arr) - max(arr))), where n is the total number of books, sum(arr) is the total number of pages in all the books and max(arr) is maximum number of pages in any book.
Auxiliary Space: O(1)

[Expected Approach] Using Binary Search

The maximum number of pages (page limit) that a student can be allocated has a monotonic property:

  • If at a page limit p, books cannot be allocated to all k students, then we need to reduce the page limit to ensure more students receive books.
  • If at a page limit p, we can allocate books to more than k students, then we need to increase the page limit so that fewer students are allocated books.

Therefore, we can apply binary search to minimize the maximum pages a student can be allocated. To check the number of students that can be allotted books for any page limit, we start assigning books to the first student until the page limit is reached, then move to the next student.


Output
113

Time Complexity: O(n × log(sum(arr) - max(arr))), where n is the total number of books, sum(arr) is the total number of pages in all the books and max(arr) is maximum number of pages in any book.
Auxiliary Space: O(1)

Comment