[LeetCode] Transform Array by Parity

3467. Transform Array by Parity

You are given an integer array nums. Transform nums by performing the following operations in the exact order specified:

  1. Replace each even number with 0.
  2. Replace each odd numbers with 1.
  3. Sort the modified array in non-decreasing order.

Return the resulting array after performing these operations.

c++
1
2
3
4
5
6
7
8
9
class Solution {
public:
vector<int> transformArray(vector<int>& nums) {
for(int i = nums.size() - 1, odd = nums.size() - 1; i >= 0; i--) {
if(nums[i] &= 1) swap(nums[i], nums[odd--]);
}
return nums;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/03/02/PS/LeetCode/transform-array-by-parity/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Related Issues not found

Please contact @SongHayoung to initialize the comment