#K35757. Equalizing Array Elements
Equalizing Array Elements
Equalizing Array Elements
You are given T test cases. For each test case, you are given an integer N followed by an array of N integers. Your task is to compute the minimum number of operations required to make all elements equal. In one operation, you can either add 1 or subtract 1 from any element. The optimal strategy is to transform all elements into the median of the array. Mathematically, if the array is represented as \(a_1, a_2,\dots,a_N\) and the median is \(m\), then the minimum operations required is:
[ \sum_{i=1}^{N} |a_i - m| ]
Note: When the array has an even number of elements, choosing any number between the two middle values will yield the same minimum cost.
inputFormat
The first line contains an integer T representing the number of test cases. Each test case consists of two lines. The first line of a test case contains an integer N denoting the number of elements in the array. The second line contains N space-separated integers representing the array elements.
outputFormat
For each test case, output a single line containing the minimum number of operations required to equalize all elements of the array.
## sample1
3
5 5 5
0
</p>