#K76467. Minimum Operations to Equalize Array Elements

    ID: 34649 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Array Elements

Minimum Operations to Equalize Array Elements

You are given an array of integers. In one operation, you can adjust two elements by increasing the smaller value by 1 and decreasing the larger value by 1. This operation effectively reduces the difference between the maximum and minimum elements by 2. Your task is to determine the minimum number of such operations required to make all elements in the array equal.

Mathematically, if \(M = \max(\texttt{arr})\) and \(m = \min(\texttt{arr})\), the minimum number of operations is given by:

[ \text{operations} = \left\lfloor \frac{M - m}{2} \right\rfloor ]

Note that the operations are defined in such a way that in each operation the overall sum of the array remains unchanged. It has been observed that the optimal strategy (if possible) will result in the above formula.

inputFormat

The input is read from standard input (stdin). The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers.

outputFormat

Print a single integer to standard output (stdout), representing the minimum number of operations needed to equalize all the elements in the array. The result is computed as (\left\lfloor \frac{\max(\texttt{arr}) - \min(\texttt{arr})}{2} \right\rfloor).## sample

3
1 2 3
1

</p>