#C6541. Perfectly Balanced Rearrangement
Perfectly Balanced Rearrangement
Perfectly Balanced Rearrangement
You are given an array of n integers. Your task is to determine whether the array can be rearranged so that the number of even elements is equal to the number of odd elements.
An array is considered perfectly balanced if it satisfies the condition \[ |even| = |odd| \] where \(|even|\) is the count of even numbers and \(|odd|\) is the count of odd numbers in the array.
If the condition is met, print YES
; otherwise, print NO
.
inputFormat
The first line of input contains a single integer n representing the number of elements in the array. The second line contains n space-separated integers, each representing an element of the array.
Note: The array may be rearranged arbitrarily to check if a perfectly balanced arrangement is possible.
outputFormat
Output a single string: YES
if the array can be rearranged to have equal numbers of even and odd elements, and NO
otherwise.
4
2 1 4 3
YES