Array Counting Sorting 3467. Transform Array by Parity ¶ Time: $O(n)$ Space: $O(n)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public: vector<int> transformArray(vector<int>& nums) { vector<int> ans; vector<int> count(2); for (const int num : nums) ++count[num % 2]; ans.insert(ans.end(), count[0], 0); ans.insert(ans.end(), count[1], 1); return ans; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17class Solution { public int[] transformArray(int[] nums) { int[] ans = new int[nums.length]; int[] count = new int[2]; for (final int num : nums) ++count[num % 2]; for (int i = 0; i < count[0]; ++i) ans[i] = 0; for (int i = count[0]; i < nums.length; ++i) ans[i] = 1; return ans; } } 1 2 3 4class Solution: def transformArray(self, nums: list[int]) -> list[int]: return ([0] * sum(num % 2 == 0 for num in nums) + [1] * sum(num % 2 == 1 for num in nums))