![]() |
VOOZH | about |
There is a school in a village. It has N classes. One fine day, someone donated B blueberry cheesecakes to schools. Now you need to divide these cakes such that:
Examples:
Input: N = 1, B = 2, ClassList = 35
Output: 18
Explanation: The number of students in each cake will be 17 and 18, and the maximum of them is 18.Input: N = 2, B = 7, ClassList = 20 50
Output: 10
Explanation: The number of cakes will be 2 for the first and 5 for the second class, so there will be a maximum of 10 students for each cake.
Source: Directi Interview | Set 8 (Off-Campus)
Basic Approach: The basic way to solve the problem is as follows:
Below is the implementation of the above idea.
18
Time Complexity: O(N * B),
Auxiliary Space: O(N), where N is number of classes and B is number of blueberry cakes.
Efficient Approach: To solve the problem using Binary Search follow the below idea:
Instead of distributing cakes one by one, we can set a value for the "maximum students per cake" and then check if it's possible to distribute the cakes in a way that meets this value. If we can achieve this distribution, we'll increase the "maximum students per cake" and try again. Conversely, if we can't achieve the desired distribution, we'll decrease the "maximum students per cake" and attempt to find a feasible distribution.
Below is the implementation of the above idea.
18
Time Complexity: O(N * log R),
Auxiliary Space: O(N), where N is number of classes and R is maximum student count among classes.