#C11111. Minimum Operations to Equalize Array
Minimum Operations to Equalize Array
Minimum Operations to Equalize Array
You are given an array of positive integers \(A = [a_1, a_2, \dots, a_n]\). In one operation, you can choose an element \(a_i\) and reduce it by replacing it with \(\frac{a_i}{g}\), where \(g\) is the greatest common divisor (GCD) of all elements in the array provided that \(a_i\) is divisible by \(g\). However, the allowed operation in this problem is conceptual: the goal is to determine the minimum number of operations required to make all elements equal. If it is not possible to make all elements equal using such operations, output \(-1\).
Explanation:
- Let \(g = \gcd(a_1, a_2, \dots, a_n)\). Then if \(g\) is not equal to \(a_1\) (i.e. the first element) the transformation is considered impossible and the answer is \(-1\).
- If possible, the number of operations needed is \(\sum_{i=1}^{n} \left(\frac{a_i}{g} - 1\right)\).
Note that you should read the input from standard input (stdin) and print the output to standard output (stdout).
inputFormat
The first line contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers: \(a_1, a_2, \dots, a_n\).
outputFormat
Output a single integer which is the minimum number of operations required to make all elements equal, or \(-1\) if it is not possible.
## sample5
1 1 1 1 1
0