#C5295. Minimum Steps to Equalize Array
Minimum Steps to Equalize Array
Minimum Steps to Equalize Array
You are given an array of N
integers. In one step, you can increment or decrement any element by 1. Your task is to compute the minimum number of steps required to make all elements of the array equal.
It can be shown that setting all elements to the median of the array minimizes the total number of operations, i.e., $$\text{steps} = \sum_{i=1}^{N} |a_i - m|,$$ where \(m\) is the median of the array. Note that if the number of elements is even, any number between the two middle values works as an optimal target.
Input: The first line contains an integer N
denoting the number of elements. The second line contains N
space-separated integers representing the array elements.
Output: Print a single integer, the minimum number of steps required.
inputFormat
The input is read from standard input. The first line contains an integer N
(1 ≤ N ≤ 10^5). The second line contains N
space-separated integers, where each integer a[i]
satisfies |a[i]| ≤ 10^6
.
outputFormat
Output a single integer — the minimum number of steps required to equalize all elements of the array.
## sample4
1 2 3 6
6
</p>