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 = 3
Output: [3,-2,1,-4]
Explanation:
[1, 3].k = 3 results in:
[1, 3] -> [3, 1] -> [1, 3] -> [3, 1][3, -2, 1, -4].Example 2:
Input: nums = [-3,-2,7], k = 1
Output: [-3,-2,7]
Explanation:
[7].k = 1 results in [7].[-3, -2, 7].Example 3:
Input: nums = [5,4,-9,6], k = 2
Output: [6,5,-9,4]
Explanation:
[5, 4, 6].k = 2 results in [6, 5, 4].[6, 5, -9, 4].
Constraints:
1 <= nums.length <= 105-105 <= nums[i] <= 1050 <= k <= 105Problem Overview: You are given an array of integers. Only the non-negative elements should be rotated among themselves while negative values stay fixed at their original indices. The relative order of negative elements never changes; only the non-negative values move to the next available non-negative position in a cyclic manner.
Approach 1: Brute Force Simulation (O(n^2) time, O(1) space)
Iterate through the array and, for each non-negative element, search forward to locate the next index containing another non-negative value. Swap or shift elements step by step until the rotation effect is achieved. Because each search may scan a large part of the array, this method can degrade to quadratic time in the worst case. The approach is easy to reason about but inefficient for large arrays.
Approach 2: Index Collection + Rotation (O(n) time, O(k) space)
Scan the array once and record the indices of all non-negative elements in a list. Extract their values, perform a cyclic rotation (for example, shift right by one), then write the rotated values back to the same recorded indices. Since each element is processed a constant number of times, the overall complexity stays linear. This technique works well because it separates position tracking from value manipulation.
Approach 3: In-Place Cyclic Rotation (O(n) time, O(1) space)
Instead of storing values separately, keep track of the first non-negative value and move through the collected non-negative indices, swapping values as you go. Each step moves the previous value into the next non-negative position, effectively simulating a cycle. This avoids extra memory and still processes the array in a single pass over the relevant indices. It’s a classic simulation pattern applied to an array.
Recommended for interviews: The index-collection simulation is the most straightforward explanation and runs in O(n) time. Interviewers usually expect this approach because it clearly separates identifying valid positions from performing the rotation. Starting with the brute-force idea shows understanding, but implementing the linear-time simulation demonstrates strong control over array traversal and state management.
We first extract all non-negative elements from the array and store them in a new array t.
Then, we create an array d of the same size as t to store the rotated non-negative elements. For each element t[i] in t, we place it in d at position ((i - k) bmod m + m) bmod m, where m is the number of non-negative elements.
Next, we iterate through the original array nums. For each position containing a non-negative element, we replace it with the element from the corresponding position in d.
The time complexity is O(n), where n is the length of the array. The space complexity is O(m), where m is the number of non-negative elements.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n^2) | O(1) | Useful for understanding the mechanics of rotating values without extra data structures. |
| Index Collection + Rotation | O(n) | O(k) | Best general solution. Simple to implement and easy to explain in interviews. |
| In-Place Cyclic Rotation | O(n) | O(1) | When memory usage matters and you want a fully in-place array manipulation. |
Rotate Non-Negative Elements | LeetCode 3819 | Weekly Contest 486 • Sanyam IIT Guwahati • 521 views views
Watch 9 more video solutions →Practice Rotate Non Negative Elements with our built-in code editor and test cases.
Practice on FleetCode