#K10851. Equalizing Array Elements

    ID: 23338 Type: Default 1000ms 256MiB

Equalizing Array Elements

Equalizing Array Elements

Given an array of integers, determine the minimum number of operations required to make all the elements equal. In one operation, you can either increase or decrease any element by 1.

You are allowed to perform the following operation on any element \(a_i\): \[ \text{new } a_i = a_i \pm 1 \] The goal is to minimize the total number of operations such that all \(a_i\) become equal. It can be shown that aligning all elements to the median minimizes the total cost, i.e., \[ \min_{m} \sum_{i=1}^{n} |a_i - m| \quad \text{with } m \text{ as the median} \]

inputFormat

The first line contains an integer (n) ((1 \le n \le 10^5)), representing the number of elements. The second line contains (n) integers separated by spaces.

outputFormat

Output a single integer — the minimum number of operations needed to make all the elements equal.## sample

3
1 2 3
2