#K1371. Minimum Moves to Equalize Skyscraper Heights
Minimum Moves to Equalize Skyscraper Heights
Minimum Moves to Equalize Skyscraper Heights
You are given n skyscrapers with varying heights. In one move, you can increase or decrease the height of any skyscraper by 1 unit. Your task is to determine the minimum number of moves required to make all skyscrapers have the same height. The optimal solution is achieved by equalizing all heights to the median of the list.
Mathematical Formulation:
If the heights are denoted by \(h_1,h_2,\dots,h_n\) and \(m\) is the median of these heights, then the minimum number of moves is given by:
\[ \text{moves} = \sum_{i=1}^{n} |h_i - m| \]This approach guarantees the smallest sum of absolute differences.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a single integer n (1 ≤ n ≤ 105) — the number of skyscrapers.
- The second line contains n space-separated integers representing the heights of the skyscrapers.
outputFormat
Output a single integer to stdout — the minimum number of moves required to equalize all the skyscraper heights.
## sample4
1 2 3 4
4
</p>