You are given an integer array ‘ARR’ of size ‘N’, where ‘ARR[i]’ denotes the number of balls in the ‘i-th’ bag. You are also given an integer ‘M’, denoting the maximum number of operations you can perform on ‘ARR’ (the given collection of bags).
In each operation, you can do the following:
After performing the operations, let ‘X’ be the maximum number of balls in a bag. The task is to find the minimum possible value of ‘X’ and return it.
Example:ARR = [5, 7], N = 2, M = 2
Perform the following two operations on ‘ARR’:
1. Divide the bag with 7 balls into 3 and 4. New ARR = [3, 4, 5].
2. Divide the bag with 5 balls into 1 and 4. New ARR = [1, 3, 4, 4].
The bag with the maximum number of balls has 4 balls. Hence, the minimum possible value of ‘X’ is 4. Return 4 as the answer.
Note:
1. You can perform any number of operations between [0, M], both included.
2. Avoid using the 'Modulo' operator as it can cause Time Limit Exceeded.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the size of array ‘ARR’ and the maximum number of operations.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of array ‘ARR’.
Output format:
For every test case, return the minimum possible value of ‘X’.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N, M <= 10^3
1 <= ARR[i] <= 10^6
Time limit: 1 sec
2
1 2
11
4 4
1 4 14 10
4
5
Test Case 1:
ARR = [11], N = 1, M = 2
Perform the following two operations on ‘ARR’:
1. Divide the bag with 11 balls into 3 and 8. New ARR = [3, 8].
2. Divide the bag with 8 balls into 4 and 4. New ARR = [3, 4, 4].
The bag with the maximum number of balls has 4 balls. Hence, the minimum possible value of ‘X’ is 4. Return 4 as the answer.
Test Case 2:
ARR = [1, 4, 14, 10], N = 4, M = 4
Perform the following four operations on ‘ARR’:
1. Divide the bag with 14 balls into 7 and 7. New ARR = [1, 4, 7, 7, 10].
2. Divide the bag with 10 balls into 5 and 5. New ARR = [1, 4, 5, 5, 7, 7].
3. Divide the bag with 7 balls into 3 and 4. New ARR = [1, 3, 4, 4, 5, 5, 7].
4. Divide the bag with 7 balls into 3 and 4. New ARR = [1, 3, 3, 4, 4, 4, 5, 5].
The bag with the maximum number of balls has 5 balls. Hence, the minimum possible value of ‘X’ is 5. Return 5 as the answer.
2
3 8
4 2 1
2 2
5 9
1
5
Iterate through all possible values of ‘x’.
Let 'X' be the maximum number of balls in a bag that we desire. Let ‘COUNT[i]’ be the number of operations that we need to perform on ‘ARR[i]’ such that the new bags made from it contain less than or equal to 'X' balls. Consider the following examples:
If we want 'X' to be ‘4’, then:
‘ARR[i] = 5’, after splitting we get [4, 1], so ‘COUNT[i] = 1’
‘ARR[i] = 8’, after splitting we get [4, 4] so ‘COUNT[i] = 1’
‘ARR[i] = 9’, after splitting we get [4, 5] => [4, 4, 1] so ‘COUNT[i] = 2’
‘ARR[i] = 12’, after splitting we get [4, 8] => [4, 4, 4] so ‘COUNT[i] = 2’
‘ARR[i] = 13’, after splitting we get [4, 9] => [4, 4, 5] => [4, 4, 4, 1] so ‘COUNT[i] = 3’
‘ARR[i] = 15’, after splitting we get [4, 11] => [4, 4, 7] => [4, 4, 4, 3] so ‘COUNT[i] = 3’
After observing we can see that: ‘COUNT[i] = (ARR[i] - 1)/x’. This is because if 'X' divides ‘ARR[i]’ then we don’t need to split the last bag, see the above two examples where ‘ARR[i] = 12’ and ‘ARR[i] = 13’ for more clarity.
Let ‘OPERATIONS’ be the number of operations we need to perform on ‘ARR’ to get the desired 'X', ‘OPERATIONS = summation(COUNT[i])’. Let ‘HIGH’ be the maximum number of balls in a bag. So, 'X' varies from 1 to ‘HIGH’. We now iterate through the values of 'X' until we find a value that has ‘OPERATIONS’ less than or equal to ‘m’. Following are the steps to obtain 'X':
O(N * MAX), where ‘N’ is the size of array ‘ARR’ and ‘MAX’ is the maximum value in ‘ARR’.
We run a single loop from 1 to ‘MAX’, i.e., for ‘MAX’ iterations. In each such iteration, we loop over ‘ARR’ to compute ‘OPERATIONS’ in ‘O(N)’. Hence, the time complexity ‘O(N * MAX).
O(1).
Since we are not using any extra space, space complexity is constant.