#C3388. Minimum Moves to Equal Array Elements
Minimum Moves to Equal Array Elements
Minimum Moves to Equal Array Elements
You are given an array of n integers. In one move, you can increment or decrement any element by 1. Your task is to compute the minimum number of moves required to make all elements equal. It can be shown that the optimal strategy is to move all the numbers to the median of the array. Mathematically, the answer is computed as:
$$moves = \sum_{i=1}^{n} |a_i - median|$$
The program should read the input from stdin
and write the output to stdout
.
inputFormat
The first line contains an integer n
representing the number of elements in the array. The second line contains n
space-separated integers representing the array elements.
outputFormat
Output a single integer which is the minimum number of moves required to make all elements equal.
## sample3
1 2 3
2