medium

Sort Array By Parity II

Rearrange an array so every even index holds an even value and every odd index holds an odd value.

1. Define the problem

Sort Array By Parity II

Given an array of integers nums, half of the integers in nums are odd, and the other half are even. Sort the array so that whenever numsi is odd, i is odd , and whenever numsi is even, i is even . Return any answer array that satisfies this condition. Use two pointers, one scanning even indices and one scanning odd indices , swapping values whenever the even-index pointer finds an odd value and the odd-index pointer finds an even value.

Constraints

  • 2 ≤ nums.length ≤ 2 × 104
  • nums.length is even.
  • half of the integers in nums are even.
  • 0 ≤ numsi ≤ 1000

Example

Inputnums = [4, 2, 5, 7]
Output[4, 5, 2, 7]

Explanation Indices 0 and 2 (even indices) hold even values 4 and 2; indices 1 and 3 (odd indices) hold odd values 5 and 7 — swapping the misplaced pair at indices 1 and 2 produces this arrangement.

2. Visualize the solution

Two pointers scan even and odd indices separately

Two pointers scan even and odd indices separately
Statusinit

evenIdx=0 (value 4) is already even — advance evenIdx by two.

What happens in this step

evenIdx = 0, oddIdx = 1
  nums[evenIdx] = nums[0] = 4

4 % 2 === 0 → already correct for an even index.
evenIdx += 2 → evenIdx becomes 2. oddIdx doesn't move this step.
Step 1 of 3

Steps to visualize

  1. Point evenIdx at index 0 and oddIdx at index 1.
  2. If numsevenIdx is already even, move evenIdx forward by two.
  3. If numsoddIdx is already odd, move oddIdx forward by two.
  4. Otherwise numsevenIdx is odd and numsoddIdx is even, so swap them and advance both pointers by two.
  5. Stop once evenIdx passes the end of the array.
3. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Two pointers scan even and odd indices separately
Statusinit

evenIdx=0 (value 4) is already even — advance evenIdx by two.

What happens in this step

evenIdx = 0, oddIdx = 1
  nums[evenIdx] = nums[0] = 4

4 % 2 === 0 → already correct for an even index.
evenIdx += 2 → evenIdx becomes 2. oddIdx doesn't move this step.
Step 1 of 3
4. Solution

Solution

solution.tsTypeScript
function sortArrayByParityII(nums) {
  let even = 0;
  let odd = 1;
  const n = nums.length;

  while (even < n && odd < n) {
    if (nums[even] % 2 === 0) {
      even += 2;
    } else if (nums[odd] % 2 === 1) {
      odd += 2;
    } else {
      const temp = nums[even];
      nums[even] = nums[odd];
      nums[odd] = temp;
      even += 2;
      odd += 2;
    }
  }

  return nums;
}
Time
O(n)
Space
O(1) (in place, excluding the returned array)
5. Test cases

Test cases

InputExpectedCovers
nums = [4, 2, 5, 7][4, 5, 2, 7]example from the docstring
nums = [1, 2][2, 1]smallest valid input: only two elements
nums = [0, 1, 2, 3][0, 1, 2, 3]already correctly arranged input needs no swaps
nums = [2, 4, 1, 3][2, 1, 4, 3]input that needs exactly one swap
nums = [1, 3, 5, 2, 4, 6][2, 3, 6, 1, 4, 5]a larger array needing multiple swaps
nums = [-1, -4, -3, -6][-4, -1, -6, -3]negative values mixed among even and odd