Transform Array by Parity - Problem

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 number with 1.

3. Sort the modified array in non-decreasing order.

Return the resulting array after performing these operations.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,1,2,4]
Output: [0,0,1,1]
💡 Note: Transform: 3→1 (odd), 1→1 (odd), 2→0 (even), 4→0 (even). After sorting: [0,0,1,1]
Example 2 — All Even
$ Input: nums = [2,4,6]
Output: [0,0,0]
💡 Note: All numbers are even, so all become 0. Sorted result: [0,0,0]
Example 3 — All Odd
$ Input: nums = [1,3,5]
Output: [1,1,1]
💡 Note: All numbers are odd, so all become 1. Sorted result: [1,1,1]

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Transform Array by Parity - Complete ProcessInput: [3, 1, 2, 4]3124Transform: Even→0, Odd→11100Sort in non-decreasing order0011Output: [0, 0, 1, 1]
Understanding the Visualization
1
Input
Integer array with mixed even/odd values
2
Transform
Replace even→0, odd→1
3
Sort
Arrange in non-decreasing order
Key Takeaway
🎯 Key Insight: Since we only get 0s and 1s, counting is more efficient than sorting
Asked in
Microsoft 25 Google 20 Amazon 15
28.1K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen