#K79002. Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
You are given an array of n integers. In one operation, you can choose any two distinct elements and replace one of them with the value of the other. Your goal is to make all elements in the array identical by performing the minimum number of operations.
A key observation is that if you choose the element which occurs most frequently, you only need to change the remaining elements. Thus, the minimum number of operations required is given by \[ \text{operations} = n - \max_{x}(\text{count}(x)) \] where \(\max_{x}(\text{count}(x))\) is the frequency of the most common element in the array.
You are to process multiple test cases. For each test case, compute and output the minimum number of operations required.
inputFormat
The first line of input contains an integer T (the number of test cases).
Each test case consists of two lines:
- The first line contains an integer n, denoting the number of elements in the array.
- The second line contains n space-separated integers.
outputFormat
For each test case, output a single line with one integer, representing the minimum number of operations required to make all array elements equal.
## sample1
4
1 2 3 4
3
</p>