
One dish can be prepared by only one cook, however, two or more cooks can simultaneously prepare different dishes.
Let ‘N’ = 4, ‘ranks’ = [1, 2, 3, 4] and ‘M’ = 11. Then the minimum time required to cook 11 dishes will be 12 minutes. The cooks should prepare dishes in the following manner -:
Cook-0 prepare 4 dishes in 10 minutes i.e (1 dish in 1 minute, 1 more dish in next 2 minutes, 1 more dish in next 3 minutes, 1 more dish in next 4 minutes).
Cook-1 prepare 3 dishes in 12 minutes i.e (1 dish in 2 minutes, 1 more dish in 4 minutes, 1 more dish in 6 minutes).
Cook-2 prepare 2 dishes in 9 minutes i.e (1 dish in 3 minutes, 1 more dish in the next 6 minutes).
Cook-3 prepare 2 dishes in 12 minutes i.e (1 dish in 4 minutes, 1 more dish in the next 8 minutes).
If all four cooks work simultaneously then they can prepare(4 + 3 + 2 + 2 = 11) dishes in 12 minutes. And it is the minimum possible time.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2*T lines represent the ‘T’ test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ representing the number of cooks and dishes respectively.
The second line of the test case contains ‘N’ space-separated integers representing ‘ranks’ of cooks
For each test case, print a line consisting of a single integer that represents the minimum times required to complete the order of ’M’ dishes.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 20
1 <= M <= 10^4
1 <= rank[i] <= 10
Time Limit: 1 sec
For each time duration starting from one, we count how many dishes each of the cooks can make, If at any duration the total number of dishes is more or equal to ‘M’ then this duration will be the minimum time required to cook ‘M’ dishes.
Let ‘T’ time is required for a cook having rank ‘R’ to prepare ‘K’ dishes then
It implies, K^2 + K - 2*T/R = 0
Or K = (-1 + sqrt(1 + (8*T)/R))/2 (Solution of quadratic equation)
i.e A cook with rank ‘R’ can prepare floor of (-1 + sqrt(1 + (8*T)/R))/2 dishes in ‘T’ times.
We can use this to optimize approach-1 as follows.
We can observe that if ‘M’ dishes cannot be prepared in ‘T’ times then they also cannot be prepared at any time between 1 to ‘T’. This suggests that we can apply binary search in time-space. We can also notice, that answer will be between 1 and 10*(M*(M+1))/2 where 10 is the maximum value of rank and ‘M’ is the number of dishes to prepare.
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers