Loading problem...
You are given an integer array nums with an equal count of even and odd numbers. Your task is to rearrange the elements of the array such that the parity of each element aligns with the parity of its index position.
Specifically:
Since the array contains exactly half even and half odd numbers, a valid rearrangement is always guaranteed to exist. You may return any valid arrangement that satisfies the above conditions.
nums = [4,2,5,7][4,5,2,7]After rearranging: index 0 → 4 (even at even index), index 1 → 5 (odd at odd index), index 2 → 2 (even at even index), index 3 → 7 (odd at odd index). Other valid outputs include [4,7,2,5], [2,5,4,7], and [2,7,4,5].
nums = [2,3][2,3]The array already satisfies the condition: 2 (even) is at index 0 (even), and 3 (odd) is at index 1 (odd). No rearrangement is needed.
nums = [1,2,3,4][2,1,4,3]The original array has odd values at even indices. After rearranging: 2 and 4 are moved to even indices (0 and 2), while 1 and 3 occupy the odd indices (1 and 3).
Constraints