#922

Sort Array By Parity II

newbie · 270 · lc easy +19 · verified · 71.2% accepted · 2,810 likes · top 80%

Description

Given an integer array nums with equally many even and odd values, rearrange it so that indices 0, 2, 4, ... hold even values and indices 1, 3, 5, ... hold odd values. Return any valid rearrangement.

Example 1:

Input: nums = [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Example 2:

Input: nums = [2,3]
Output: [2,3]

Code

1
2
3