Watch 10 video solutions for Transform Array by Parity, a easy level problem involving Array, Sorting, Counting. This walkthrough by code kural has 1,414 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums. Transform nums by performing the following operations in the exact order specified:
Return the resulting array after performing these operations.
Example 1:
Input: nums = [4,3,2,1]
Output: [0,0,1,1]
Explanation:
nums = [0, 1, 0, 1].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:
nums = [1, 1, 1, 0, 0].nums in non-descending order, nums = [0, 0, 1, 1, 1].
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1000Problem Overview: You are given an integer array and need to transform it so that elements are grouped by parity. All even numbers should appear before odd numbers in the final array. The relative order usually does not matter, so the goal is simply to separate values based on whether num % 2 == 0.
Approach 1: Sorting by Parity (O(n log n) time, O(1) space)
A straightforward way is to sort the array using a custom comparator that prioritizes even numbers over odd numbers. Most languages allow sorting with a key such as num % 2, which naturally places even values first. This works because even numbers evaluate to 0 and odd numbers to 1. The approach relies on standard sorting algorithms, which typically run in O(n log n) time and use constant or logarithmic extra space depending on implementation. It is simple but slower than necessary for this problem.
Approach 2: Two-Pointer Partition (O(n) time, O(1) space)
You can treat the array like a partition problem. Maintain two pointers: one from the start and one from the end. Move the left pointer forward while it points to even numbers, and move the right pointer backward while it points to odd numbers. When an odd number appears on the left and an even number on the right, swap them. This approach processes each element at most once, making it linear time. The idea is similar to partitioning used in quicksort and works well for problems involving arrays and in-place rearrangement.
Approach 3: Counting Evens and Odds (O(n) time, O(1) extra space)
The counting strategy is the simplest when you only need the final parity grouping. First iterate through the array and count how many elements are even. Then overwrite the array: fill the first evenCount positions with even numbers and the remaining positions with odd numbers while scanning the original data. This approach relies on a simple counting technique and sequential iteration. Because it performs two linear passes and only stores a few counters, it runs in O(n) time with constant space.
Recommended for interviews: The two-pointer or counting approach is what interviewers expect. Sorting shows you understand the requirement but wastes time complexity. Demonstrating an O(n) partition or counting solution shows stronger control over array traversal and in-place transformations.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting by parity | O(n log n) | O(1) | Quick implementation when performance is not critical |
| Two-pointer partition | O(n) | O(1) | Best in-place method when order does not matter |
| Counting evens and odds | O(n) | O(1) | Simplest logic when you just need grouped parity |