
The first line of the input contains an integer, 'T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'K', denoting the number of elements in the array 'ARR', and the special number respectively.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
For each test case, print the minimum number of transactions needed to make the wallet balance of all the friends equal.
Print the output of each test case in a new line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= K <= 10^4
0 <= ARR[i] <= 10^4
Where 'ARR[i]' represents the ith element of 'ARR'.
Time Limit: 1 sec
So our first idea is to check whether the average balance is an integer. If it is not an integer, then we will return -1. As we can transact in integer values only. The other condition to check is that for each friend, the difference of his initial wallet balance and the average wallet balance should be a multiple of ‘K’. If it is not, then we will return -1.Otherwise, it is guaranteed that an answer exists.
This works because we are allowed to transact in multiples of ‘K’ only. This means that the wallet balance of a friend increases or decreases in multiples of 'K' only. Therefore, it is necessary that the difference between initial wallet balance and the average wallet balance of friends is multiple of ‘K’.
To find the transactions, we can iterate through all the friends having an initial balance greater than the average balance and count how many transactions are required for each friend so that their wallet balance becomes exactly equal to the average balance. Our final answer will be the total number of transactions that are carried out.