#C8643. Rearranging Boxes for Even Adjacent Sums
Rearranging Boxes for Even Adjacent Sums
Rearranging Boxes for Even Adjacent Sums
You are given an integer (n) and a sequence of (n) integers written on boxes. Your task is to determine whether it is possible to rearrange these boxes in a line such that for every two adjacent boxes, the sum of the numbers is even. In other words, for every pair ((a, b)) of adjacent boxes, the condition [ a+b \equiv 0 \pmod{2} ] must hold.
A key observation is that the sum of two integers is even if and only if both integers have the same parity (i.e., both are even or both are odd). Therefore, a valid rearrangement exists if and only if all the numbers are even, or all of them are odd.
inputFormat
The first line of input contains an integer (n) ((1 \leq n \leq 10^5)), the number of boxes. The second line contains (n) space-separated integers, where the (i^{th}) integer represents the number written on the (i^{th}) box. Each integer is within the range ([-10^9, 10^9]).
outputFormat
Output a single line containing the string YES
if it is possible to rearrange the boxes such that the sum of the numbers in every adjacent pair is even. Otherwise, output NO
.## sample
4
2 4 6 8
YES