3467. Transform Array by Parity LeetCode Solution

In this guide, you will get 3467. Transform Array by Parity LeetCode Solution with the best time and space complexity. The solution to Transform Array by Parity problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Transform Array by Parity solution in C++
  4. Transform Array by Parity solution in Java
  5. Transform Array by Parity solution in Python
  6. Additional Resources
3467. Transform Array by Parity LeetCode Solution image

Problem Statement of Transform Array by Parity

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.

Example 1:

Input: nums = [4,3,2,1]
Output: [0,0,1,1]
Explanation:

Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, nums = [0, 1, 0, 1].
After sorting nums in non-descending order, nums = [0, 0, 1, 1].

Example 2:

Input: nums = [1,5,1,4,2]
Output: [0,0,1,1,1]
Explanation:

Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, nums = [1, 1, 1, 0, 0].
After sorting nums in non-descending order, nums = [0, 0, 1, 1, 1].

Constraints:

1 <= nums.length <= 100
1 <= nums[i] <= 1000

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)
See also  2434. Using a Robot to Print the Lexicographically Smallest String LeetCode Solution

3467. Transform Array by Parity LeetCode Solution in C++

class 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;
  }
};
/* code provided by PROGIEZ */

3467. Transform Array by Parity LeetCode Solution in Java

class 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;
  }
}
// code provided by PROGIEZ

3467. Transform Array by Parity LeetCode Solution in Python

class 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))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.