Battalions and tanks
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.
Input Format :
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.
Constraints :
1 <= T <= 50
1 <= ‘N’ <= 1000
‘N’ <= ‘M’ <= 10000
1 <= ARR[i] <= 1000000
Time limit: 1 sec
CodingNinjas
author
2y
Max-heap based approach.
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...read more
CodingNinjas
author
2y
Binary search based approach.
The basic idea of this approach is to find the minimum ratio using a binary search.
Let us see how to check if a given real number (say ‘X’) is the minimum ratio(as define...read more
Help your peers!
Add answer anonymously...
Top Amazon Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Amazon Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app