Book Allocation Problem in Python

In this problem, we will be given a number of books, let's say N, and a number of students, let's say M. Along with this, we are given the number of pages each book contains. The array that contains the number of pages is sorted in ascending order. Our task is to assign each student books such that the maximum number of pages that we have assigned to a student is the minimum number of pages. Also, we need to take care that the books are assigned in consecutive order and not randomly. At last, we have to print the minimum number of pages assigned to a student. Let us see some examples to understand the problem.

Example:

Input: N = 5, pages[] = [10, 20, 50, 80, 100], M = 2

Output: 160

Explanation

  • Let us start by giving student 1 the first book and student 2 the rest of the books, i.e., [20, 50, 80, 100]. In this case, the number of pages with student 1 and student 2 are 10 and 20 + 50 + 80 + 100 = 250, respectively.
  • Now let us give student 1 the first 2 books and student 2 the rest of the books, i.e., [10, 20] for student 1 and [50, 80, 100] for student 2. In this case, the number of pages with student 1 and student 2 is 30 and 50 + 80 + 100 = 230, respectively.
  • Now let us give student 1 the first 3 books and the rest to student 2, i.e., [10, 20, 50] for student 1 and [80, 100] for student 2. In this case, the number of pages with student 1 and student 2 are 80 and 80 + 100 = 180, respectively.
  • Now let us give student 1 all of the books except the last one, which will be with student 2, i.e., [10, 20, 50, 80] for student 1 and [100] for student 2. In this case, the number of pages with student 1 and student 2 are 10 + 20 + 50 + 80 = 160 and 100, respectively.
  • Hence, the maximum number of pages allotted to each student is minimum when student 1 is given books [10, 20, 50, 80] and student 2 is given books [100].

The maximum number of pages allotted to a student, which is a minimum number of pages, is 160.

Approach - 1

In this approach, we will find all the permutations of the books. From those permutations, we will calculate the maximum number of pages allotted to a student. Then, we can find the permutation for which the maximum number is the minimum. However, this approach is not efficient as the time complexity to find all the permutations of the books will be very large. This approach will not work efficiently for a large number of books.

Code

Output:

The answer is: 160

Approach - 2

We will modify the above approach to improve the time complexity. In the above-mentioned approach, we used a for loop to perform a linear search on the possible range of the page count values. We can reduce the time taken by the linear search by using the Binary Search algorithm. The binary search algorithm will perform the same task in log N time complexity, where N is the range of the page count values.

Here is the algorithm to solve this problem:

  • We will find the middle page count values using the current low and high page count values. Then, we will pass this middle value to a function that will check if we can allocate all of the student's books with the current minimum page count.
  • If the allocation is possible, then we will try to reduce the range by updating the higher value and putting it equal to the middle value - 1
  • If the allocation is not possible, then we will increase the range because, with the current minimum count, not all students are getting books. Hence, we will put the lower range value equal to the middle value + 1.
  • We will stop once the range values overlap or cross each other.

Below is the implementation of this approach.

Code

Output:

The answer is: 160