Mid/SeniorArraySimulation

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
Inputnums = [1,-2,3,-4], k = 3
Output[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
Inputnums = [-3,-2,7], k = 1
Output[-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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate