50. Sort Array by Parity

EasyArrayArray

Given an integer array nums, move all even integers to the beginning and all odd integers to the end. Return any array that satisfies this condition.

Input: An integer array nums.

Output: Array with all even elements before all odd elements.

Examples

Example 1
Input: [3,1,2,4]
Output: [2,4,3,1]
Explanation: Even: [2,4]. Odd: [3,1]. One valid answer: [2,4,3,1].
Example 2
Input: [0]
Output: [0]
Explanation: Single even element. Already valid.
Example 3
Input: [1,2,3,4,5,6]
Output: [2,4,6,1,3,5]
Explanation: Evens first: [2,4,6]. Odds after: [1,3,5].

Constraints

Asked by

MetaBloombergGoogleAmazonMicrosoft
Solve this problem in the editor →