Back to problems Sign in to unlock
Leetcode
Easy
Arrays
Sorting
Sort Array By Parity
Reorder an array so that all even numbers come before all odd numbers.
Acceptance 0%
Problem Statement
Given an integer array, rearrange its elements so that every even number appears before every odd number. The relative order within the even group and the odd group does not need to be preserved unless a follow-up asks for stability.
Return any valid reordered array.
Input Format
- An integer array
nums. - Each element may be positive, negative, or zero.
Output Format
- Return the same array rearranged so that all even elements appear before all odd elements.
Constraints
- The array length can be zero or more.
- A valid arrangement must place every even number before every odd number.
- Stability is not required for the standard version of the problem.
Examples
Sample cases returned by the problem API.
Example 1
Input
nums = [3,1,2,4]
Output
[2,4,3,1]
Explanation
The even numbers 2 and 4 are moved to the front, followed by the odd numbers 3 and 1.
Example 2
Input
nums = [0]
Output
[0]
Explanation
Zero is even, so the array is already valid.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.