Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,-2,3,-4] 3
[-3,-2,7] 1
[5,4,-9,6] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3819: Rotate Non Negative Elements
class Solution {
public int[] rotateElements(int[] nums, int k) {
int m = 0;
for (int x : nums) {
if (x >= 0) {
m++;
}
}
int[] t = new int[m];
int p = 0;
for (int x : nums) {
if (x >= 0) {
t[p++] = x;
}
}
int[] d = new int[m];
for (int i = 0; i < m; i++) {
d[((i - k) % m + m) % m] = t[i];
}
int j = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] >= 0) {
nums[i] = d[j++];
}
}
return nums;
}
}
// Accepted solution for LeetCode #3819: Rotate Non Negative Elements
func rotateElements(nums []int, k int) []int {
t := make([]int, 0)
for _, x := range nums {
if x >= 0 {
t = append(t, x)
}
}
m := len(t)
d := make([]int, m)
for i, x := range t {
d[((i-k)%m+m)%m] = x
}
j := 0
for i, x := range nums {
if x >= 0 {
nums[i] = d[j]
j++
}
}
return nums
}
# Accepted solution for LeetCode #3819: Rotate Non Negative Elements
class Solution:
def rotateElements(self, nums: List[int], k: int) -> List[int]:
t = [x for x in nums if x >= 0]
m = len(t)
d = [0] * m
for i, x in enumerate(t):
d[((i - k) % m + m) % m] = x
j = 0
for i, x in enumerate(nums):
if x >= 0:
nums[i] = d[j]
j += 1
return nums
// Accepted solution for LeetCode #3819: Rotate Non Negative Elements
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3819: Rotate Non Negative Elements
// class Solution {
// public int[] rotateElements(int[] nums, int k) {
// int m = 0;
// for (int x : nums) {
// if (x >= 0) {
// m++;
// }
// }
// int[] t = new int[m];
// int p = 0;
// for (int x : nums) {
// if (x >= 0) {
// t[p++] = x;
// }
// }
// int[] d = new int[m];
// for (int i = 0; i < m; i++) {
// d[((i - k) % m + m) % m] = t[i];
// }
// int j = 0;
// for (int i = 0; i < nums.length; i++) {
// if (nums[i] >= 0) {
// nums[i] = d[j++];
// }
// }
// return nums;
// }
// }
// Accepted solution for LeetCode #3819: Rotate Non Negative Elements
function rotateElements(nums: number[], k: number): number[] {
const t: number[] = nums.filter(x => x >= 0);
const m = t.length;
const d = new Array<number>(m);
for (let i = 0; i < m; i++) {
d[(((i - k) % m) + m) % m] = t[i];
}
let j = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] >= 0) {
nums[i] = d[j++];
}
}
return nums;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.