#C7601. Minimum Operations to Equalize Array
Minimum Operations to Equalize Array
Minimum Operations to Equalize Array
You are given an array of integers. In a single operation, you may choose any contiguous subarray and replace every element in that subarray with the floor of the average of its elements. Formally, for a subarray with sum S and length L, every element is replaced by \(\lfloor \frac{S}{L} \rfloor\). The goal is to make all array elements equal using as few operations as possible.
It can be observed that if you repeatedly perform the optimal operation, the minimal number of operations needed is equal to the total number of elements minus the frequency of the most common element. This is because you can leave the most frequent element unchanged while adjusting the others.
Input Constraint: The array is 1-indexed, but indexing is not necessary for solving the problem.
For instance, consider the array \([1, 2, 3, 4, 5]\). The most frequent element occurs once; hence, the answer is \(5 - 1 = 4\).
inputFormat
The first line of input contains an integer \(T\) denoting the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
Input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing the minimum number of operations required to equalize the array. Output is written to standard output (stdout).
## sample3
5
1 2 3 4 5
4
4 4 4 4
6
10 10 30 30 10 10
4
0
2
</p>