Rotate Non Negative Elements
You are given an integer array nums and an integer k.
Rotate only the non-negative elements of the array to the left by k positions, in a cyclic manner.
All negative elements must stay in their original positions and must not move.
After rotation, place the non-negative elements back into the array in the new order, filling only the positions that originally contained non-negative values and skipping all negative positions.
Return the resulting array.
Example 1
Input
nums = [1,-2,3,-4], k = 3Output
[3,-2,1,-4]The non-negative elements are
[1, 3], and rotating them left by 3 gives [3, 1], which are placed back into the original non-negative positions.Example 2
Input
nums = [-3,-2,7], k = 1Output
[-3,-2,7]The only non-negative element is
[7], so rotating it left by 1 keeps the array unchanged.Constraints
- 1 <= nums.length <= 10^5
- -10^5 <= nums[i] <= 10^5
- 0 <= k <= 10^5