Talentd
FleetCode

3467. Transform Array by Parity

Easy89.7% acceptance1 company

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.

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

Asked at 1 company

Hints open one at a time

Hint 1
Let x be the number of even numbers, and y be the number of odd numbers. Output 0 x times, followed by 1 y times.
3:58
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def transformArray(self, nums: List[int]) -> List[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
[4,3,2,1]
Input
[1,5,1,4,2]
Your code is saved on this device.Solution