Description
Given an array of integers nums, half of which are odd and half are even, sort the array so that nums[i] is odd when i is odd, and nums[i] is even when i is even.
Examples
Input:
nums = [4,2,5,7]Output:
[4,5,2,7]Explanation:
Evens at 0,2; odds at 1,3.
Input:
nums = [1,2,3,4,6,8]Output:
[2,1,4,3,6,8]Explanation:
Original array has evens [2,4,6,8] and odds [1,3]. Evens go at indices 0,2,4 and odds at indices 1,3,5 to satisfy the parity requirement.
Input:
nums = [10,11,12,13,14,15,16,17]Output:
[10,11,12,13,14,15,16,17]Explanation:
The array is already correctly arranged with evens [10,12,14,16] at even indices 0,2,4,6 and odds [11,13,15,17] at odd indices 1,3,5,7. No rearrangement needed.
Constraints
- •
2 ≤ nums.length ≤ 2 × 10⁴