#K8211. Minimum Operations to Equal Array Elements
Minimum Operations to Equal Array Elements
Minimum Operations to Equal Array Elements
You are given an array a
of n integers. In one operation, you can select any two elements and increase both of them by 1.
Your task is to determine the minimum number of operations required to make all the elements of the array equal.
Explanation: It can be shown that the optimal solution is obtained by effectively reducing the differences between each element and the smallest element in the array. The required operations can be computed using the formula \[ \text{operations} = \sum_{i=1}^{n} (a_i - \min(a)), \] where \(\min(a)\) is the minimum element in the array.
For example, for the array [1, 2, 3], the answer is \((1-1) + (2-1) + (3-1) = 3\) operations.
inputFormat
The first line of input contains a single integer n (the number of elements). The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single integer: the minimum number of operations required to make all array elements equal.## sample
3
1 2 3
3