Allocate Minimum Number of Pages17 Mar 2025 | 3 min read Problem statementYou are given an array of integers of size N, which represents the number of pages in N books. You are also given one integer M which represents the number of students. You have to distribute the N books to these M students for reading in such a way that every student has to read the book, and all the books should be read. One student can read more than one book also, but the books should be in continuous order. The order of the books will be ascending order according to the number of pages. Your task is to distribute the books among the students in such a way that the maximum number of pages allocated to a student should be a minimum. For example: Let N=5 and M=3 Pages[] = { 120,132,165,324,987} In the above example, we can have the following possibilities: Case1:Case3:Case3:Case4:Case5:Here, we will use binary search to get the solution to the problem. We have a lot of edge cases in this problem like:
So as we know, we apply the binary search where we have the range of possible answers. In this problem, the minimum answer would be the maximum value of the array, and the maximum answer would be the sum of the array. Java Code:Output: ![]() Explanation In the above code, we have implemented the binary search solution for minimum page allocation. We have the page array, which contains five values, and we have m equal to 2. For binary search left = 987 and right = sum (page) = 1728 mid= 987+(1728-987)/2 = 1357 Now for the value mid, we will check that it is possible to allocate the pages in such a way that the maximum pages allocated to a student should be less than the mid. So we will get true, and now our left=987 and right will be mid-1 which will be 1356. Now mid= 987+(1356-987)/2 = 1171 So now, for the mid value 1171, we will get the true from the function, and we will move towards the left into search space. So right will be 1170 now. Mid = 987+(1170-987)/2 = 1078 So now for value mid, we will get true, and we will move towards left, so right=1077 Mid=987+(1077-987)/2 = 1032 So we will true for 1032, so we will go towards left again. So right = 1031 Mid= 987+(1031-987)/2 = 1009 So we will return true again now, right = 1008 And mid= 987+(1008-987)/2 = 997 And function will return true for 997, so the right will be 996. Mid = 987+(996-987)/2 = 991 And it is also true, so right = 990 Now mid = 987+(990-987)/2=988 And again, true, so right = 987 Now mid =987, and we will get true, so right=986 but left 987, so the loop will be terminated, and mid =987 will remain the answer. Time complexity: O(logN), where N represents the total number of pages in the book. Space complexity: O(1) Next TopicAssembly Line Scheduling |
The level of a key in a specific binary tree generally refers to the distance that is present between the root of the binary tree node and the node that is containing the desired key. It is very important and noteworthy how many steps are required...
7 min read
A depth-first search (DFS) is a method of traversing graphs that is similar to tree preorder traversal. The following is a recursive implementation of preorder traversal: Depth First Traversal (or Search) for a graph is same as Depth First Traversal (DFS) for a tree. The only point...
3 min read
Introduction Finding the optimal beginning position for collecting coins in a grid is a typical task in a variety of computational applications. One such issue includes a grid with a fixed amount of coins in each cell. The aim is to choose the ideal cell to begin...
6 min read
Merging two sorted arrays is a popular procedure in computer science. The difficulty emerges when you are charged with combining these arrays in place with no extra space allocation. This issue frequently arises in interviews and real-world circumstances when memory is a crucial restriction. Let's look...
9 min read
When working with data structures like arrays or linked lists, we often need to compare or correlate the elements within them. Searching for pairs that meet criteria, detecting loops, or reversing order are common tasks. These can be done naively with nested loops, which can be...
9 min read
Introduction: Graphs are a fundamental data structure used in computer science to model relationships between objects. One common problem associated with graphs is cycle detection, which involves determining whether there is a closed path (cycle) within the graph. Cycle detection is crucial in various applications: Network routing Deadlock detection Topological...
6 min read
This article aims to facilitate your comprehension of Reservoir Sampling in C++ by presenting an algorithmic explanation accompanied by illustrative code. The content encompasses the fundamentals of Reservoir Sampling, featuring a practical use case, a detailed algorithm walkthrough, and a hands-on C++ implementation with a corresponding...
4 min read
The problem is to check whether the given binary number is divisible by 3 or a multiple of 3. This problem is very popular in the programming world and asked in software engineering interviews by none other than Amazon, Microsoft, Adobe, etc. The binary number can be in...
15 min read
Problem Statement We are given an integer array deck where deck[i] represents the number written on the ith card. Partition the cards into one or more groups such that: Each group has exactly x cards where x > 1, and All the cards in one group have the same integer...
5 min read
The Chinese Postman or Route Inspection Problem is a type of Eulerian circuit problem that finds the shortest closed path in an undirected graph such that every edge is visited at least once. This problem is also very relevant in cases when a postman needs to...
7 min read
We request you to subscribe our newsletter for upcoming updates.
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