Book Allocation Problem

Yukti Kumari
Oct 22, 2021

Introduction

In this article, we will solve the problem: Book Allocation. We will start with a quite naive and intuitive approach and then proceed towards a more optimal binary search solution.

Problem Statement

Given an array ‘pages’ of integer numbers, where ‘pages[i]’ represents the number of pages in the ‘i-th’ book. There are ‘m’ number of students, and the task is to allocate all the books to their students.

Allocate books in a way such that:

1. Each student gets at least one book.

2. Each book should be allocated to a student.

3. Book allocation should be in a contiguous manner.

You have to allocate the books to ‘m’ students such that the maximum number of pages assigned to a student is minimum.

Let’s understand the problem statement through an example.

Example:

Number of books = 4 and Number of students = 2

pages[] = { 10,20,30,40}

Important points to consider

• Assign at least one book to every student, so there can’t be any allocation such that a student gets no books assigned.
• While allocating the books, no book should be left out. In other words, you have to allocate each and every book given.
• Allocate in a contiguous manner. Let's say you have to allocate 3 books to a student from pages[] = { 10,20,30,40}

Then, the possible allocations can be - {10,20,30} and {20,30,40}. You can’t allocate {10,30,40} as it is not contiguous.

All possible ways of book allocation are shown in the below figure-

The minimum of the maximum number of pages assigned = min{90,70,60} = 60. Hence, the required answer is 60.

Brute Force Approach

Let be the number of books and the number of students.

In the case of M > N:

Simply return -1 as the number of students is greater than the number of books available, and according to the given constraints, it is not possible to allocate at least one book to each student.

Let’s see the algorithm for all other cases where M<=N:

• We have to minimize the value of the maximum number of pages assigned to a student in an allocation.
• If the maximum number of pages assigned to a student in a book allocation is max_pages, then this implies that the number of pages assigned to every student is less than or equal to max_pages.
• Let’s focus on the possible values of the number of pages that can be assigned to a student?
• The minimum value can be equal to 0 (Though not in this problem because you have to assign each student at least one book, so the number of pages can't be zero)
• The maximum number of pages will be the sum of the number of pages in all books. (This will happen when you assign all the books to one student).
• The range of the maximum number of pages we obtained is - (0, sum of all the values of pages array]. Open interval on 0 because at least one book needs to be assigned to every student. So, the interval we get is [1, sum of pages array].

Steps to be followed are

1. Iterate over all values ranging from numPages=1 to numPages=sum of pages array.
2. Check if it is possible to allocate the books such that the value of the number of pages assigned to any student is less than or equal to numPages in each iteration.
3. If the maximum number of pages can be equal to numPages, then return numPages as the answer. We don’t need to iterate further because after this the value of numPages will increase, but we are interested in finding the minimum value of the maximum number of pages that can be allocated.

How to check if it is possible to allocate the books such that the maximum number of pages assigned to any student is numPages?

1. Initialize count of students with 1.
2. Keep allocating the books to a student until the sum of the pages assigned is less than numPages.
3. If at any point the number of pages assigned to a student exceeds numPages, then allocate the current book to the next student and increment the count of students.
4. If the count of students becomes greater than M, then return false.
5. In the end, if the count of students is equal to M, return true.

Output

Time Complexity - O(n*Sum)

O(n*Sum), where ‘n’ is the number of integers in the array ‘pages’ and ‘Sum’ is the sum of all the elements of ‘pages’.

We are using two nested loops of size ‘Sum’ and ‘n’. So, the time complexity is O(n*Sum).

Space Complexity - O(1)

O(1) as we are using constant space.

Binary Search Approach

The idea is to use binary search over the search space [1, Sum of pages array] to improve the time complexity.

Algorithm

• Initially ‘start = 0’ and ‘end = sum of all pages’
• Find mid = (start+end)/2
• Check if it is possible to allocate books such that the number of pages allocated to each student is less than or equal to mid.
• If possible then:
• Update the minimum answer found so far and put end mid-1.
• Since we need the minimum value of the maximum number of pages, the end is set as mid-1 so that our search space now becomes [start,mid-1] to get a value less than mid.

• Else
• Update start mid+1. Because if it is not possible to allocate the books with maximum pages equal to mid then it won’t be possible for any value less than mid. So, the search space becomes [mid+1,end].
• Repeat the above steps until start <= end.

Output

Time Complexity - O(N*log(Sum))

O(N*log(Sum)), where ‘N’ is the number of integers in the array ‘pages’ and ‘Sum’ is the sum of all the elements of ‘pages’ as for every number ‘mid’ we have an iteration loop of size ‘N’ and binary search takes ‘log(Sum)’ time.

Space Complexity - O(1)

O(1) as we are using constant space.

Key Takeaways

In this article, we discussed the Book Allocation problem to find the minimum value of the maximum number of pages assigned to a student.

This question is one of the excellent applications of Binary Search.  We discussed the naive approach first and then optimized the solution by binary search as the problem had monotonic property.

With this problem, you must have got an idea of when and how to use binary search.

Some of the problems which you can practice based on a similar concept are-

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? Attempt our Online Mock Test Series on CodeStudio now!