Sort Array By Parity II

Easy
2
42.9% Acceptance

In this lab, you will be given an array of integers nums, where half of the integers in nums are odd, and the other half are even. Your task is to sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even. You should return any answer array that satisfies this condition.

For example:

  • If nums is [4,2,5,7], a valid output would be [4,5,2,7]. Other valid outputs like [4,7,2,5], [2,5,4,7], or [2,7,4,5] would also be accepted.
  • If nums is [2,3], the output would be [2,3].

Constraints:

  • 2 <= nums.length <= 2 * 104
  • nums.length is even.
  • Half of the integers in nums are even.
  • 0 <= nums[i] <= 1000

Follow Up: Could you solve it in-place?

Challenges

  1. Export the function sortArrayByParityII from index.js.
  2. The exported sortArrayByParityII function should sort the input array according to the parity condition described.
  3. The function sortArrayByParityII should work with the given constraints and edge cases.