#K82947. 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 elements equal. In each move, you can increment or decrement an element by 1. An optimal strategy is to make every element equal to the median of the array. Formally, if the sorted array is (a_1, a_2, \ldots, a_n) and the median is (x = a_{\lfloor n/2 \rfloor + 1}) (using 1-indexing), then the required moves is (\sum_{i=1}^{n} |a_i - x|). Note that if the array is empty or consists of one element, the answer is 0.
inputFormat
The first line of input contains an integer (n) (where (n \ge 0)) representing the number of elements in the array. The second line contains (n) space-separated integers.
outputFormat
Output a single integer that represents the minimum number of moves required to make all elements of the array equal.## sample
3
1 2 3
2