#K47652. Minimum Moves to Make Equal
Minimum Moves to Make Equal
Minimum Moves to Make Equal
You are given an array of n integers. In one move, you can increment or decrement any element by 1. Your task is to determine the minimum number of moves required to make all elements of the array equal. The optimal strategy is to change all elements to the median of the array.
Mathematically, if the array is a1, a2, ..., an and m is the median, then the answer is given by:
$$ \sum_{i=1}^{n} |a_i - m| $$
You are required to process T independent test cases.
inputFormat
The first line contains an integer T representing the number of test cases.
Each test case consists of two lines:
- The first line contains an integer n, the number of elements in the array.
- The second line contains n space-separated integers.
outputFormat
For each test case, output a single line containing the minimum number of moves required to make all array elements equal.
## sample3
3
1 2 3
4
1 1 1 1
2
1000000000 1000000000
2
0
0
</p>