#K72057. Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
You are given an array of integers. In one operation, you can increment or decrement any element by 1. Your task is to determine the minimum number of operations required to make all elements of the array equal.
The optimal strategy is to change all elements to the median of the array. This is because if we let \(m\) be the median, then the total cost \[ C = \sum_{i=1}^{n} |a_i - m| \] is minimized.
Input Format: The first line contains an integer \(n\) (the number of elements in the array). The second line contains \(n\) space-separated integers.
Output Format: Output a single integer representing the minimum number of operations required.
Example:
Input: 4 1 3 6 9</p>Output: 11
inputFormat
The input is given via standard input. It consists of two lines:
- The first line contains a single integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array.
outputFormat
Output a single integer to standard output which is the minimum number of operations required to make all the elements equal.
## sample4
1 3 6 9
11
</p>