Back to ArraysSort Array By Parity
Easy

Sort Array By Parity

EasyOptionalTwo PointersPartitioning

Given an array of integers, rearrange it so all even numbers come before all odd numbers. Any order within each group is acceptable.

Examples

Example 1

Input:
nums = [3,1,2,4]
Output:
[4,2,1,3]

Explanation: Evens (4, 2) move to the front and odds (1, 3) move to the back; the exact order within each group is not required.

Example 2

Input:
nums = [0,1,2]
Output:
[0,2,1]

Explanation: Only one odd value exists, so it moves to the end while the two evens stay ahead of it.

Constraints

  • 1 <= nums.length <= 5000
  • 0 <= nums[i] <= 5000

Loading editor…

Code execution is coming soon.