#3379
Easy Algorithms Transformed array
Array Simulation
70.4% acceptance
Feb 24, 2026
448
46
You are given an integer array nums that represents a circular array. Your task is to create a new array result of the same size, following these rules:
For each index i (where 0 <= i < nums.length), perform the following independent actions:
If nums[i] > 0: Start at index i and move nums[i] steps to the right in the circular array. Set result[i] to the value of the index where you land.
If nums[i] < 0: Start at index i and move abs(nums[i]) steps to the left in the circular array. Set result[i] to the value of the index where you land.
If nums[i] == 0: Set result[i] to nums[i].
Return the new array result.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn construct_transformed_array(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len() as i32;
nums.iter().enumerate().map(|(i, &v)| {
if v == 0 {
0
} else {
let j = ((i as i32 + v).rem_euclid(n)) as usize;
nums[j]
}
}).collect()
}
}