#3467
Easy Algorithms Transform array by parity
Array Sorting Counting
89.8% acceptance
Feb 25, 2026
101
7
You are given an integer array nums. Transform nums by performing the following operations in the exact order specified:
Replace each even number with 0.
Replace each odd numbers with 1.
Sort the modified array in non-decreasing order.
Return the resulting array after performing these operations.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn transform_array(nums: Vec<i32>) -> Vec<i32> {
let evens = nums.iter().filter(|&&x| x % 2 == 0).count();
let odds = nums.len() - evens;
let mut res = vec![0; evens];
res.extend(vec![1; odds]);
res
}
}