Easy
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] <= 1000class Solution {
fun transformArray(nums: IntArray): IntArray {
val size = nums.size
val ans = IntArray(size)
var countEven = 0
for (i in nums.indices) {
if (nums[i] and 1 == 0) {
countEven++
}
}
for (i in countEven until size) {
ans[i] = 1
}
return ans
}
}