
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains an integer ‘N’ representing the total number of people.
The second line of each test case contains ‘N’ space-separated integers representing the initial count of candies for each person.
For each test case, print an integer representing the minimum possible total increase/decrease in candies count to make equal number of candies for each person.
The output of each test case will be printed in a separate line.
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 5000
1 <= candies[i] <= 10 ^ 9
Where ‘candies[i]’ is the initial count of candies for each person 'i' from 1 to ‘N’.
Time Limit : 1sec
Let's suppose that all people will get ‘X’ number of candies after the operations are performed. We can simply iterate over all possible ‘X’ and find the minimum total increase/decrease in the number of candies for each ‘X’. It can be easily observed that we need to check only such ‘X’ that lies between the between- person with a minimum number of candies to a person with the maximum number of candies
between-person
The idea is to take the median of the given array and make all elements of the array equal to the median. Because we can observe that it is optimal to make all candies as the middle element, in this way we need to make fewer changes in the number of candies. For example, if we have a sorted array say { a1, a2, a3, a4, a5 }, it is optimal to make all elements equal to a3, rather than any other elements like a2 or a4.
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers