#K54912. Make Array Elements Equal

    ID: 29859 Type: Default 1000ms 256MiB

Make Array Elements Equal

Make Array Elements Equal

You are given an array of (n) integers. In one operation, you can increment or decrement a single element by 1. The task is to determine the minimum number of operations required to make all elements equal.

An optimal strategy is to make every element equal to the median of the array. Mathematically, given a sorted array (a_1, a_2, \dots, a_n), choosing the median (m) minimizes the sum (\sum_{i=1}^{n} |a_i - m|).

For example, when the input array is [1, 2, 3, 4, 5], the median is 3 and the minimum number of operations is 6.

inputFormat

The first line contains a single integer (n), the size of the array. The second line contains (n) space-separated integers representing the elements of the array.

outputFormat

Output a single integer: the minimum number of operations required to make all the elements in the array equal.## sample

5
1 2 3 4 5
6