#C11602. Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
You are given 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 increment or decrement an element by 1.
The optimal strategy is to make all elements equal to the median of the array. Mathematically, if the sorted array is \(a_1, a_2, \dots, a_n\), then choosing \(m = a_{\lfloor n/2 \rfloor + 1}\) minimizes the sum \[ \sum_{i=1}^{n} |a_i - m|. \]
You are given multiple test cases.
inputFormat
The first line of input contains a single integer \(T\) representing the number of test cases. Each test case consists of two lines. The first line contains a single integer \(n\) (the number of elements in the array). The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
For each test case, print a single line containing the minimum number of operations required to make all elements equal.
## sample3
2
1 2
3
-1 0 1
4
1 3 2 2
1
2
2
</p>