#K85182. Equalizing Array Elements with GCD
Equalizing Array Elements with GCD
Equalizing Array Elements with GCD
You are given an array \(A\) of \(n\) integers. Your task is to determine the minimum number of operations required to make all elements equal. The operation is defined implicitly by the following observation:
Let \(G = \gcd(a_1, a_2, \ldots, a_n)\). If \(G \neq 1\), then the elements share a common divisor greater than 1 and can be equalized (in this problem, this means that no additional operations are needed, so the answer is 0). Otherwise, if \(G = 1\), it is impossible to equalize the elements using the allowed operation, and the answer should be \(-1\).
In summary, compute \(G = \gcd(a_1, a_2, \ldots, a_n)\). If \(G \neq 1\) output 0; otherwise, output \(-1\).
inputFormat
The first line contains an integer (n) ((1 \le n \le 10^5)), representing the number of elements in the array. The second line contains (n) space-separated integers (a_1, a_2, \ldots, a_n) (each |(a_i)| may be large) representing the elements of the array.
outputFormat
Output a single integer: 0 if the (\gcd) of the array is not equal to 1 (i.e., equalization is possible), or -1 if the (\gcd) equals 1 (i.e., it is impossible to equalize the array).## sample
4
2 4 6 8
0