← All problems

Sort Array by Parity

Easy arrayssortingtwo-pointers LeetCode ↗
Given an integer array, rearrange it so that all even numbers come before all odd numbers, and return the array. The relative order within evens/odds does not matter. Do it in place with O(1) extra space if you can.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n), space O(1).

Common pitfalls

  • Infinite loop when pointers are not advanced after a swap.
  • Using extra arrays when the in-place partition is straightforward.
  • Confusing this with a stable partition (order preservation is not required).

Your solution

Log in to save your code and track progress.