#C1088. Minimum Operations to Equalize Elements

    ID: 40133 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Elements

Minimum Operations to Equalize Elements

Given T test cases, each test case consists of an integer (n) followed by (n) integers representing an array. In one operation, you can increase or decrease an element by 1. Your task is to compute the minimum number of operations required to make all the elements equal. The optimal strategy is to choose the median of the array as the target value, because the median minimizes the sum of absolute deviations. Mathematically, if (a_1, a_2, \ldots, a_n) are the elements and (m) is the median, the minimum operations required is given by:

[ \sum_{i=1}^{n} |a_i - m| ]

Your solution should read input from standard input and output the result for each test case on a separate line.

inputFormat

The first line of the input contains an integer (T) (the number of test cases). Each of the following (T) lines represents a test case. Each test case begins with an integer (n) (the number of elements), followed by (n) space-separated integers.

outputFormat

For each test case, output a single line containing the minimum number of operations required to make all the elements equal.## sample

3
3 1 2 3
4 2 2 2 2
5 1 2 3 4 5
2

0 6

</p>