#K10806. Reorder Array: Odd Numbers Followed by Even Numbers
Reorder Array: Odd Numbers Followed by Even Numbers
Reorder Array: Odd Numbers Followed by Even Numbers
You are given an array of n integers. Your task is to reorder the array so that all odd numbers appear before all even numbers if and only if the array contains at least one odd number and at least one even number.
If the array contains both odd and even numbers, output YES
in the first line, and in the second line output the reordered sequence (all odd numbers followed by all even numbers) separated by spaces. Otherwise, output NO
on a single line.
This can be thought of as satisfying the condition that there exists at least one pair (one odd and one even) whose sum is odd. In mathematical form, if an arrangement exists then there exist indices \(i\) and \(j\) such that $$c_i+c_j \equiv 1 \pmod{2}.$$
Note: The reordering is only considered valid if both odd and even numbers are present in the input. If the array consists of all odd numbers or all even numbers (or has only one element), then it is impossible to achieve the desired arrangement.
inputFormat
The first line contains a single integer n
— the number of elements in the array.
The second line contains n
space-separated integers representing the array.
outputFormat
If the array contains both odd and even numbers, output YES
on the first line and then the reordered array (all odd numbers followed by all even numbers) on the second line with each number separated by a space.
If such reordering is impossible, output NO
on a single line.
5
1 3 2 4 5
YES
1 3 5 2 4
</p>