Let there be βNβ battalions of soldiers and βMβ tanks. You are also given an array/list of length βNβ whose i-th index denotes the number of soldiers in the i-th battalion. You are supposed to divide the βMβ tanks to βNβ battalions such that the maximum ratio of soldiers in a battalion to the number of tanks allotted to that battalion is minimised.
You can assume that the number of tanks is always greater than the number of battalions.
The first line contains an integer βTβ denoting the number of test cases. Then each test case follows.
The first input line of each test case contains two integers βNβ and βMβ denoting the number of battalions and tanks respectively.
The second line of each test case contains βNβ space-separated integers denoting the number of soldiers in βNβ battalions.
Output Format :
For each test case, return the maximum ratio of soldiers in a battalion to the number of tanks as described in the problem statement.
Your answer is considered correct if its absolute or relative error doesn't exceed 0.000001.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= βNβ <= 1000
βNβ <= βMβ <= 10000
1 <= ARR[i] <= 1000000
Time limit: 1 sec
2
3 6
1000 2000 10000
3 12
4 8 12
2500.000000
2.000000
For the first test case, we can distribute the tanks such that the first battalion gets 1 tank, the second battalion gets 1 tank, and the third battalion gets 4 tanks. In this way the ratio of soldiers in a battalion to the number of tanks allotted will be [1000, 2000, 2500] where the maximum ratio is 2500. This is the optimal distribution, and no other distribution can generate a smaller value of the maximum ratio.
For the second test case, [2, 4, 6] distribution will lead to a minimum value of maximum ratio which is 2.
2
3 3
2 5 7
1 4
8
7.000000
2.000000
For the first test case, there are only 3 tanks available so we will distribute 1 tank to each battalion.
For the second test case, there is only 1 battalion so assign all tanks to that battalion.
Can you think of using a max-heap for this task?
Let us first assign 1 tank to each battalion because the number of tanks assigned to any battalion will definitely be greater than or equal to 1. Now, if any extra tank is left then assigning the tank to the battalion having the maximum ratio of soldiers to the number of tanks will be an optimal choice. Because the maximum ratio will definitely decrease if we assign the next tank to this battalion. Similarly, we can do it for each remaining tank.
We can observe here that at each instance we are supposed to find the battalion having the maximum ratio. We can easily perform this task using a max-heap.
Consider the following steps:
O(M * log(N)), Where βNβ and βMβ denotes the number of battalions and tanks respectively.
Since we are using a max-heap where insert and extract maximum operation takes O(log(N)) time (βNβ is the number of elements in the heap). And in this approach, we are performing an insert and an extract maximum operation for each tank. So the overall complexity will be O(M * log(N)).
O(N), Where βNβ denotes the number of battalions.
Since we are using a max-heap to store the information of each battalion which takes O(N) space. So the overall space complexity will be O(N).