Tap here to switch tabs
Problem
Ask
Submissions

Problem: Sort Array By Parity II

easy
15 min
Explore how to rearrange an integer array where half the elements are even and half are odd. Learn to apply the cyclic sort pattern to place evens at even indexes and odds at odd indexes, mastering a commonly used coding interview technique.

Statement

You are given an integer, array nums, where exactly half of the elements are even, and the other half are odd.

Rearrange nums such that:

  • All even numbers are placed at even indexes [0,2,4,...][0, 2, 4, ...].

  • All odd numbers are placed at odd indexes [1,3,5,...][1, 3, 5, ...].

You may return any valid arrangement that satisfies these conditions.

Constraints:

  • 22 \leq nums.length 103 \leq 10^3

  • nums.length is even.

  • Half of the integers in nums are even.

  • 00\leq nums[i] 1000\leq1000

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Sort Array By Parity II

easy
15 min
Explore how to rearrange an integer array where half the elements are even and half are odd. Learn to apply the cyclic sort pattern to place evens at even indexes and odds at odd indexes, mastering a commonly used coding interview technique.

Statement

You are given an integer, array nums, where exactly half of the elements are even, and the other half are odd.

Rearrange nums such that:

  • All even numbers are placed at even indexes [0,2,4,...][0, 2, 4, ...].

  • All odd numbers are placed at odd indexes [1,3,5,...][1, 3, 5, ...].

You may return any valid arrangement that satisfies these conditions.

Constraints:

  • 22 \leq nums.length 103 \leq 10^3

  • nums.length is even.

  • Half of the integers in nums are even.

  • 00\leq nums[i] 1000\leq1000