#K44607. Equalizing Array via Summation Operation
Equalizing Array via Summation Operation
Equalizing Array via Summation Operation
Given an array of integers, you are allowed to perform the following operation any number of times: select any two elements and replace them with their sum. Mathematically, if you choose two elements \(a\) and \(b\), they are replaced by \(a+b\). The goal is to determine whether it is possible to make all elements in the array equal by repeatedly applying this operation.
The key observation is that the parity (even or odd nature) of the numbers plays a crucial role. In particular, if the array contains both even and odd numbers, it is impossible to make all elements equal because the parity mix cannot be eliminated. However, if all numbers are either even or all odd (or are already equal), then the answer is YES
.
For example, consider the array [1, 1, 2]
. Since it contains both odd and even numbers, the result is NO
. In contrast, for the array [2, 2, 4, 4]
, all numbers are even, so the result is YES
.
inputFormat
The input is read from stdin
and begins with an integer \(T\) (\(T \geq 1\)) representing the number of test cases. This is followed by \(T\) test cases. Each test case has the following format:
- The first line of each test case contains an integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
For each test case, output a single line containing YES
if it is possible to make all elements equal using the allowed operation, or NO
otherwise. The output should be printed to stdout
.
5
3
1 1 2
4
2 2 4 4
3
1 3 5
3
2 4 6
3
1 2 4
NO
YES
YES
YES
NO
</p>