Practice/Meta/Leetcode 905. Sort Array By Parity
CodingOptional
Given an array of integers, rearrange the elements so that all even numbers appear before all odd numbers. The relative order of elements within the even group and within the odd group doesn't matter—any valid arrangement is acceptable.
You should modify the array in-place and return it.
Example 1:
Input: [3, 1, 2, 4] Output: [2, 4, 3, 1] Explanation: All even numbers (2, 4) come before all odd numbers (3, 1). Other valid outputs include [4, 2, 1, 3], [2, 4, 1, 3], etc.
Example 2:
Input: [0] Output: [0] Explanation: A single even number is already correctly partitioned.
Example 3:
Input: [1, 3, 5, 7] Output: [1, 3, 5, 7] Explanation: When all numbers are odd, no rearrangement is needed.
Example 4:
Input: [10, 9, 8, 7, 6, 5] Output: [10, 8, 6, 9, 7, 5] Explanation: Even numbers (10, 8, 6) are grouped before odd numbers (9, 7, 5).