#K69177. 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 remove any two elements and replace them with their sum. This operation decreases the size of the array by 1. Your task is to determine the minimum number of operations needed so that all the elements in the array become equal.
Note that if the array initially contains only one element or if all elements are already equal, no operations are needed. Otherwise, the only way to ensure equality is to repeatedly merge elements until only one element remains. Hence, the answer in the latter case is given by the formula:
[ \text{Operations} = n - 1 \quad \text{for } n > 1 \text{ and not all elements are equal.} ]
If it is impossible to make the elements equal (which will not happen under the given operation), output -1
.
inputFormat
The first line of input contains a single integer T (1 ≤ T ≤ 104), representing the number of test cases. Each test case is described as follows:
- The first line contains an integer n (1 ≤ n ≤ 105), the number of elements in the array.
- The second line contains n space-separated integers representing the array elements.
It is guaranteed that the sum of n over all test cases does not exceed 106.
outputFormat
For each test case, output a single line containing the minimum number of operations required to make all elements equal.
## sample1
4
1 1 1 1
0
</p>