#C5744. Equal Array Transformation
Equal Array Transformation
Equal Array Transformation
You are given an array of integers. In one operation, you can select any two distinct numbers from the array and replace both of them with their integer average. The integer average is defined only when the sum of the two numbers is even. Therefore, this operation is only possible if both numbers have the same parity (i.e. both are even or both are odd).
Your task is to determine whether it is possible to make all elements of the array equal by performing these operations any number of times.
Mathematical Insight:
Notice that the parity of any element remains unchanged after replacing it with the average of two numbers having the same parity. Hence, a necessary and sufficient condition for the array to be transformable into an array with all equal elements is that all elements have the same remainder when divided by 2; that is, they must all be either even or odd. In \(\LaTeX\)
notation, if \(a_i\) is an element of the array, then we require:
[ a_i \bmod 2 = a_j \bmod 2 \quad \text{for all } i, j. ]
If this condition holds, output YES
; otherwise, output NO
.
inputFormat
The first line contains an integer \(T\) (\(1 \leq T \leq 10^5\)) denoting the number of test cases. Each test case is described as follows:
- The first line of each test case contains an integer \(N\) (\(1 \leq N \leq 10^5\))— the number of elements in the array.
- The second line contains \(N\) space-separated integers representing the elements of the array.
All input is given via standard input (stdin).
outputFormat
For each test case, output a single line containing either YES
if it is possible to make all elements equal using the allowed operation, or NO
if it is not possible. The output should be printed to standard output (stdout).
3
4
1 3 2 4
2
6 10
3
5 5 5
NO
YES
YES
</p>