Problem · Array
Stable Parity Partition
Learn this problemProblem statement
Given an integer array nums, return a new array containing all even values before all odd values.
The partition must be stable:
- Even values keep their relative order from
nums. - Odd values keep their relative order from
nums.
An integer is even when its remainder after division by 2 is 0. This definition also applies to negative integers and 0.
Function
partitionByParity(nums: int[]) → int[]Examples
Example 1
nums = [3,2,4,1,6]return = [2,4,6,3,1]The even subsequence is [2,4,6] and the odd subsequence is [3,1]. Concatenating them preserves both relative orders.
Example 2
nums = [-3,-2,0,5,4,-1]return = [-2,0,4,-3,5,-1]Negative parity follows the same divisibility rule. The stable even group is [-2,0,4].
Example 3
nums = []return = []An empty input has empty even and odd groups.
Constraints
0 <= nums.length <= 100000-10^9 <= nums[i] <= 10^9