Skip to content
Decoder

Sort Array By Parity

Easy

Problem

Given a list of non-negative integers nums, move all the even numbers before all the odd numbers and return the list. Any order within the evens and within the odds is accepted. Try to do it in place with O(1) extra space.

Example 1

Input
nums = [3, 1, 2, 4]
Output
[2, 4, 3, 1]
Why
[4, 2, 3, 1], [2, 4, 1, 3] and [4, 2, 1, 3] are also fine

Example 2

Input
nums = [0]
Output
[0]

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.