Book Allocation Problem in PythonIn 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
The maximum number of pages allotted to a student, which is a minimum number of pages, is 160. Approach - 1In 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 - 2We 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:
Below is the implementation of this approach. Code Output: The answer is: 160 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India