3819. Rotate Non Negative Elements Medium

@problem@discussion
#Array#Simulation



1/**
2 * [3819] Rotate Non Negative Elements
3 *
4 * You are given an integer array nums and an integer k.
5 * Rotate only the non-negative elements of the array to the left by k positions, in a cyclic manner.
6 * All negative elements must stay in their original positions and must not move.
7 * 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.
8 * Return the resulting array.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">nums = [1,-2,3,-4], k = 3</span>
13 * Output: <span class="example-io">[3,-2,1,-4]</span>
14 * Explanation:​​​​​​​
15 * 
16 * 	The non-negative elements, in order, are [1, 3].
17 * 	Left rotation with k = 3 results in:
18 * 	
19 * 		[1, 3] -> [3, 1] -> [1, 3] -> [3, 1]
20 * 	
21 * 	
22 * 	Placing them back into the non-negative indices results in [3, -2, 1, -4].
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">nums = [-3,-2,7], k = 1</span>
27 * Output: <span class="example-io">[-3,-2,7]</span>
28 * Explanation:
29 * 
30 * 	The non-negative elements, in order, are [7].
31 * 	Left rotation with k = 1 results in [7].
32 * 	Placing them back into the non-negative indices results in [-3, -2, 7].
33 * </div>
34 * <strong class="example">Example 3:
35 * <div class="example-block">
36 * Input: <span class="example-io">nums = [5,4,-9,6], k = 2</span>
37 * Output: <span class="example-io">[6,5,-9,4]</span>
38 * Explanation:
39 * 
40 * 	The non-negative elements, in order, are [5, 4, 6].
41 * 	Left rotation with k = 2 results in [6, 5, 4].
42 * 	Placing them back into the non-negative indices results in [6, 5, -9, 4].
43 * </div>
44 *  
45 * Constraints:
46 * 
47 * 	1 <= nums.length <= 10^5
48 * 	-10^5 <= nums[i] <= 10^5
49 * 	0 <= k <= 10^5
50 * 
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/rotate-non-negative-elements/
55// discuss: https://leetcode.com/problems/rotate-non-negative-elements/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn rotate_elements(nums: Vec<i32>, k: i32) -> Vec<i32> {
61        vec![]
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_3819() {
73    }
74}
75

Back
© 2026 bowen.ge All Rights Reserved.