#K72482. Equalize the Array
Equalize the Array
Equalize the Array
Given an array of integers, you are allowed to perform operations where in one operation you can increment or decrement an element by 1. The goal is to determine the minimum total number of operations required to make all elements in the array equal.
An optimal strategy is to change all elements to the median of the array, because this minimizes the total number of operations. Mathematically, if the target value is the median, then the number of operations is given by $$\sum_{i=1}^{n} |a_i - \text{median}|.$$
You are given multiple test cases. For each test case, compute and output the minimum number of operations required to make all the elements equal.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- The first line contains an integer
t
, the number of test cases. - For each test case, the first line contains an integer
n
representing the number of elements in the array. - The next line contains
n
space-separated integers representing the elements of the array.
outputFormat
For each test case, output a single line containing the minimum number of operations required to make all elements in the array equal.
The output should be printed to standard output (stdout).
## sample3
5
1 2 3 4 5
4
10 10 10 10
3
2 8 3
6
0
6
</p>