|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3467.Transform%20Array%20by%20Parity/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3467. Transform Array by Parity](https://leetcode.com/problems/transform-array-by-parity) |
| 10 | + |
| 11 | +[中文文档](/solution/3400-3499/3467.Transform%20Array%20by%20Parity/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given an integer array <code>nums</code>. Transform <code>nums</code> by performing the following operations in the <strong>exact</strong> order specified:</p> |
| 18 | + |
| 19 | +<ol> |
| 20 | + <li>Replace each even number with 0.</li> |
| 21 | + <li>Replace each odd numbers with 1.</li> |
| 22 | + <li>Sort the modified array in <strong>non-decreasing</strong> order.</li> |
| 23 | +</ol> |
| 24 | + |
| 25 | +<p>Return the resulting array after performing these operations.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong class="example">Example 1:</strong></p> |
| 29 | + |
| 30 | +<div class="example-block"> |
| 31 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,3,2,1]</span></p> |
| 32 | + |
| 33 | +<p><strong>Output:</strong> <span class="example-io">[0,0,1,1]</span></p> |
| 34 | + |
| 35 | +<p><strong>Explanation:</strong></p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li>Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, <code>nums = [0, 1, 0, 1]</code>.</li> |
| 39 | + <li>After sorting <code>nums</code> in non-descending order, <code>nums = [0, 0, 1, 1]</code>.</li> |
| 40 | +</ul> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p><strong class="example">Example 2:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,5,1,4,2]</span></p> |
| 47 | + |
| 48 | +<p><strong>Output:</strong> <span class="example-io">[0,0,1,1,1]</span></p> |
| 49 | + |
| 50 | +<p><strong>Explanation:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li>Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, <code>nums = [1, 1, 1, 0, 0]</code>.</li> |
| 54 | + <li>After sorting <code>nums</code> in non-descending order, <code>nums = [0, 0, 1, 1, 1]</code>.</li> |
| 55 | +</ul> |
| 56 | +</div> |
| 57 | + |
| 58 | +<p> </p> |
| 59 | +<p><strong>Constraints:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li><code>1 <= nums.length <= 100</code></li> |
| 63 | + <li><code>1 <= nums[i] <= 1000</code></li> |
| 64 | +</ul> |
| 65 | + |
| 66 | +<!-- description:end --> |
| 67 | + |
| 68 | +## Solutions |
| 69 | + |
| 70 | +<!-- solution:start --> |
| 71 | + |
| 72 | +### Solution 1: Counting |
| 73 | + |
| 74 | +We can traverse the array $\textit{nums}$ and count the number of even elements $\textit{even}$. Then, we set the first $\textit{even}$ elements of the array to $0$ and the remaining elements to $1$. |
| 75 | + |
| 76 | +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. |
| 77 | + |
| 78 | +<!-- tabs:start --> |
| 79 | + |
| 80 | +#### Python3 |
| 81 | + |
| 82 | +```python |
| 83 | +class Solution: |
| 84 | + def transformArray(self, nums: List[int]) -> List[int]: |
| 85 | + even = sum(x % 2 == 0 for x in nums) |
| 86 | + for i in range(even): |
| 87 | + nums[i] = 0 |
| 88 | + for i in range(even, len(nums)): |
| 89 | + nums[i] = 1 |
| 90 | + return nums |
| 91 | +``` |
| 92 | + |
| 93 | +#### Java |
| 94 | + |
| 95 | +```java |
| 96 | +class Solution { |
| 97 | + public int[] transformArray(int[] nums) { |
| 98 | + int even = 0; |
| 99 | + for (int x : nums) { |
| 100 | + even += (x & 1 ^ 1); |
| 101 | + } |
| 102 | + for (int i = 0; i < even; ++i) { |
| 103 | + nums[i] = 0; |
| 104 | + } |
| 105 | + for (int i = even; i < nums.length; ++i) { |
| 106 | + nums[i] = 1; |
| 107 | + } |
| 108 | + return nums; |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +#### C++ |
| 114 | + |
| 115 | +```cpp |
| 116 | +class Solution { |
| 117 | +public: |
| 118 | + vector<int> transformArray(vector<int>& nums) { |
| 119 | + int even = 0; |
| 120 | + for (int x : nums) { |
| 121 | + even += (x & 1 ^ 1); |
| 122 | + } |
| 123 | + for (int i = 0; i < even; ++i) { |
| 124 | + nums[i] = 0; |
| 125 | + } |
| 126 | + for (int i = even; i < nums.size(); ++i) { |
| 127 | + nums[i] = 1; |
| 128 | + } |
| 129 | + return nums; |
| 130 | + } |
| 131 | +}; |
| 132 | +``` |
| 133 | +
|
| 134 | +#### Go |
| 135 | +
|
| 136 | +```go |
| 137 | +func transformArray(nums []int) []int { |
| 138 | + even := 0 |
| 139 | + for _, x := range nums { |
| 140 | + even += x&1 ^ 1 |
| 141 | + } |
| 142 | + for i := 0; i < even; i++ { |
| 143 | + nums[i] = 0 |
| 144 | + } |
| 145 | + for i := even; i < len(nums); i++ { |
| 146 | + nums[i] = 1 |
| 147 | + } |
| 148 | + return nums |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +#### TypeScript |
| 153 | + |
| 154 | +```ts |
| 155 | +function transformArray(nums: number[]): number[] { |
| 156 | + const even = nums.filter(x => x % 2 === 0).length; |
| 157 | + for (let i = 0; i < even; ++i) { |
| 158 | + nums[i] = 0; |
| 159 | + } |
| 160 | + for (let i = even; i < nums.length; ++i) { |
| 161 | + nums[i] = 1; |
| 162 | + } |
| 163 | + return nums; |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +<!-- tabs:end --> |
| 168 | + |
| 169 | +<!-- solution:end --> |
| 170 | + |
| 171 | +<!-- problem:end --> |
0 commit comments