#K13706. Minimum Operations to Equalize Heights
Minimum Operations to Equalize Heights
Minimum Operations to Equalize Heights
You are given an integer \(N\) and an array of \(N\) integers representing the heights of different elements. In one operation, you can either increase or decrease the height of an element by 1. Your task is to determine the minimum number of operations required to make all the heights equal.
The optimal strategy is to make all elements equal to the median of the array. This is because the median minimizes the sum of absolute deviations. In other words, if you choose a value \(h\) as the target, the total number of operations required is \(\sum_{i=1}^{N} |h_i - h|\). Setting \(h\) to the median yields the minimum possible sum.
Note: The input includes \(N\) and a list of \(N\) integers. The output is a single integer representing the minimum number of operations.
inputFormat
The first line contains a single integer \(N\) (the number of elements).
The second line contains \(N\) space-separated integers representing the array of heights.
outputFormat
Output a single integer: the minimum number of operations required to make all heights equal.
## sample3
3 3 3
0
</p>