#K88267. Minimum Moves to Equal Array Elements
Minimum Moves to Equal Array Elements
Minimum Moves to Equal Array Elements
Given an array of integers, your task is to determine the minimum number of moves required to make all the array elements equal. In one move, you can increment or decrement any element by 1. The optimal strategy is to convert all elements to the median of the array.
The number of moves is given by the formula:
$$\sum_{i=1}^{n} |a_i - m|$$
where \(m\) is the median value of the array. This problem needs to be solved for multiple 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 of each test case contains an integer
n
, the number of elements in the array. - The second line contains
n
space-separated integers.
For example:
2 3 1 2 3 5 10 20 30 40 50
outputFormat
For each test case, output a single line containing the minimum number of moves required to make all the elements equal.
## sample2
3
1 2 3
5
10 20 30 40 50
2
60
</p>