every position that held an odd value still holds an odd value, and the odd values appear in ascending order from left to right;
every position that held an even value still holds an even value, and the even values appear in descending order from left to right.
In other words, the odd values are sorted among themselves, the even values are sorted among themselves, and the parity pattern of positions is unchanged. Zero is even; parity is determined by the value (-3 is odd). Return the rearranged array.
Examples
Example 1
Input: nums = [9,4,3,8,7,2,5]
Output: [3,8,5,4,7,2,9]
Explanation: The odd values 9,3,7,5 sit at positions 0,2,4,6 and become 3,5,7,9 there; the even values 4,8,2 sit at positions 1,3,5 and become 8,4,2.
Example 2
Input: nums = [2,4,6]
Output: [6,4,2]
Explanation: All values are even, so the whole array is sorted in descending order.
Example 3
Input: nums = [-1,0,-3,5]
Output: [-3,0,-1,5]
Explanation: The odd values -1,-3,5 ascend to -3,-1,5; the single even value 0 stays.
Constraints
1≤nums.length≤105
−109≤nums[i]≤109
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 3
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run