Search⌘ K
AI Features

Solution: Sort Array By Parity II

Understand how to rearrange an array where half the integers are even and half odd so that even elements are at even indexes and odd elements at odd indexes. Learn a modified cyclic sort approach with two pointers to efficiently accomplish this in-place with O(n) time and O(1) space.

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 ...