#K92642. Minimum Operations to Equalize Array
Minimum Operations to Equalize Array
Minimum Operations to Equalize Array
You are given an array of n integers. Your task is to determine the minimum number of operations required to make all elements of the array equal. In one operation, you can increment or decrement any element by 1.
The optimal solution is achieved by making all elements equal to the median of the array. This minimizes the total number of operations since the median minimizes the sum of absolute deviations.
Note: The input is taken from stdin
and the output should be written to stdout
.
inputFormat
The first line of input contains an integer n, the number of elements in the array. The second line contains n space-separated integers.
outputFormat
Output a single integer which is the minimum number of operations required to make all the elements equal.
## sample4
1 2 3 4
4
</p>