On your birthday, your friends gifted you 'N' boxes of chocolates, which are represented as an array 'A' of 'N' positive integers where 'A[i]' denotes the number of chocolates in the i-th box. You want to give these boxes as return gifts to your guests but with an equal number of chocolates in each box. For that, you are going to perform the following operation on the boxes, where each operation is as follows:
Choose a positive integer 'x'.
If any of the boxes contains more than 'x' chocolates, take out the extra chocolates from that box, i.e., if 'A[i]' > 'x', make 'A[i]' = 'x' ( for all 'i' where 1 <= 'i' <= 'N')
The cost of an operation is the total number of chocolates taken out from all the boxes, i.e., cost = (sum of 'A' before operation) - (sum of 'A' after operation).
Your task is to find the minimum number of operations such that there is an equal number of chocolates in each box and the cost of each operation does not exceed 'K'.
Note :1. 'x' can be any positive integer of your choice. It may or may not be present in array 'A'.
2. 'K' is large enough to have at least one 'x' for the operation.
Example :
N = 4
K = 5
A = [ 5, 2, 3, 4 ]
In the first operation, choose x = 3, the updated array 'A' becomes [3, 2, 3, 3]. The cost of this operation is (5+2+3+4)-(3+2+3+3) = 3.
In the second operation, choose x = 2, the updated array 'A' becomes [2, 2, 2, 2]. The cost of this operation is (3+2+3+3)-(2+2+2+2) = 3.
After the second operation, all elements in array 'A' become equal. Hence return 2 as our final answer.
We can't choose x = 2 in our first operation, because (5+2+3+4)-(2+2+2+2) = 6 which exceeds K = 5.
The first line contains an integer 'T', denoting the number of test cases.
Then the test cases follow:
The first line of each test case contains two space-separated integers, 'N' and 'K', denoting the number of boxes (or size of array 'A') and maximum cost of an operation, respectively.
The next line contains 'N' integers, denoting the number of chocolates in each box (or array 'A' elements).
Output Format :
For each test case, print an integer denoting the minimum number of operations needed to perform on array 'A' to make all elements equal.
Print the output of each test case in a new line.
Note :
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= 'T' <= 10
1 <= 'N' <= 10^5
N <= 'K' <= 10^9
1 <= 'A[i]' <= 10^5
Sum of 'N' over all test cases doesn’t exceed 10^5.
Time Limit: 1 sec
2
5 6
3 1 3 6 3
5 5
2 2 2 2 3
3
1
For test case 1:
In the first operation, choose x = 3, the updated array 'A' becomes [3, 1, 3, 3, 3]. The cost of this operation is (3+1+3+6+3)-(3+1+3+3+3) = 3.
In the second operation, choose x = 2, the updated array 'A' becomes [2, 1, 2, 2, 2]. The cost of this operation is (3+1+3+3+3)-(2+1+2+2+2) = 4.
In the third operation, choose x = 1, the updated array 'A' becomes [1, 1, 1, 1, 1]. The cost of this operation is (2+1+2+2+2)-(1+1+1+1+1) = 4.
After the third operation, all elements in array 'A' become equal. Hence return 3 as our final answer.
For test case 2:
In the first operation, choose x = 2, the updated array 'A' becomes [2, 2, 2, 2, 2]. The cost of this operation is (2+2+2+2+3)-(2+2+2+2+2) = 1.
After the first operation, all elements in array 'A' become equal. Hence return 1 as our final answer.
3
5 7
3 3 3 3 3
4 8
4 3 2 1
4 4
1 5 3 7
0
1
4
The chosen value of 'x' will be strictly decreasing in continuous operations.
Approach:
Our task is to make all elements in the array equal. The optimal way is to make the rest of the 'N-1' elements equal to the smallest element of the array.
Algorithm:
O(N logN), where 'N' is the number of elements in array 'A'.
Sorting an array of 'N' size takes O(N * logN). After that we traversed the array only once. Hence overall time complexity becomes O(N logN).
O(1)
We are using only constant space to store various sums and differences. Hence the overall space complexity becomes O(1).