Problem

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.

Note: Since nums is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end.

Example 1

1
2
3
4
5
6
7
Input: nums = [3,-2,1,1]
Output: [1,1,1,3]
Explanation:
* For `nums[0]` that is equal to 3, If we move 3 steps to right, we reach `nums[3]`. So `result[0]` should be 1.
* For `nums[1]` that is equal to -2, If we move 2 steps to left, we reach `nums[3]`. So `result[1]` should be 1.
* For `nums[2]` that is equal to 1, If we move 1 step to right, we reach `nums[3]`. So `result[2]` should be 1.
* For `nums[3]` that is equal to 1, If we move 1 step to right, we reach `nums[0]`. So `result[3]` should be 3.

Example 2

1
2
3
4
5
6
Input: nums = [-1,4,-1]
Output: [-1,-1,4]
Explanation:
* For `nums[0]` that is equal to -1, If we move 1 step to left, we reach `nums[2]`. So `result[0]` should be -1.
* For `nums[1]` that is equal to 4, If we move 4 steps to right, we reach `nums[2]`. So `result[1]` should be -1.
* For `nums[2]` that is equal to -1, If we move 1 step to left, we reach `nums[1]`. So `result[2]` should be 4.

Constraints

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Examples

Solution

Method 1 – Simulation

Intuition

For each index, simulate the movement according to the rules, using modulo to wrap around the circular array.

Approach

Iterate through each index. If nums[i] > 0, move right; if nums[i] < 0, move left; if nums[i] == 0, just copy. Use modulo to handle circular movement.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <vector>
using namespace std;
class Solution {
public:
    vector<int> transformedArray(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n);
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 0) res[i] = nums[i];
            else {
                int j = (i + nums[i] + n) % n;
                res[i] = nums[j];
            }
        }
        return res;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int[] transformedArray(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 0) res[i] = nums[i];
            else {
                int j = (i + nums[i] + n) % n;
                res[i] = nums[j];
            }
        }
        return res;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from typing import List
class Solution:
    def transformedArray(self, nums: List[int]) -> List[int]:
        n = len(nums)
        res = [0] * n
        for i in range(n):
            if nums[i] == 0:
                res[i] = nums[i]
            else:
                j = (i + nums[i]) % n
                res[i] = nums[j]
        return res
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
impl Solution {
    pub fn transformed_array(nums: Vec<i32>) -> Vec<i32> {
        let n = nums.len();
        let mut res = vec![0; n];
        for i in 0..n {
            if nums[i] == 0 {
                res[i] = nums[i];
            } else {
                let j = ((i as i32 + nums[i]).rem_euclid(n as i32)) as usize;
                res[i] = nums[j];
            }
        }
        res
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function transformedArray(nums: number[]): number[] {
    const n = nums.length;
    const res = new Array(n);
    for (let i = 0; i < n; ++i) {
        if (nums[i] === 0) res[i] = nums[i];
        else {
            let j = (i + nums[i]) % n;
            if (j < 0) j += n;
            res[i] = nums[j];
        }
    }
    return res;
}

Complexity

  • ⏰ Time complexity: O(n)
  • 🧺 Space complexity: O(n)