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:
i (where 0 <= i < nums.length), perform the following independent actions:
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 at the index where you land.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 at the index where you land.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:
Input: nums = [3,-2,1,1]
Output: [1,1,1,3]
Explanation:
nums[0] that is equal to 3, If we move 3 steps to right, we reach nums[3]. So result[0] should be 1.nums[1] that is equal to -2, If we move 2 steps to left, we reach nums[3]. So result[1] should be 1.nums[2] that is equal to 1, If we move 1 step to right, we reach nums[3]. So result[2] should be 1.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:
Input: nums = [-1,4,-1]
Output: [-1,-1,4]
Explanation:
nums[0] that is equal to -1, If we move 1 step to left, we reach nums[2]. So result[0] should be -1.nums[1] that is equal to 4, If we move 4 steps to right, we reach nums[2]. So result[1] should be -1.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] <= 100Problem Overview: You are given an integer array nums. For each index i, move nums[i] steps in the array (right for positive values, left for negative values) using circular indexing. The value at the destination index becomes result[i]. The task is to build this transformed array.
Approach 1: Step-by-Step Simulation (O(n²) time, O(n) space)
The most direct approach simulates movement one step at a time. For every index i, repeatedly move left or right depending on the sign of nums[i]. Since the array is circular, wrap around when the pointer moves past either end. After completing the required number of steps, record the element at the final index. This method mirrors the problem statement closely and is easy to reason about, but repeated stepping can make the total work quadratic in the worst case.
Approach 2: Direct Circular Index Calculation (O(n) time, O(n) space)
You can compute the final position directly using modular arithmetic instead of moving step by step. For index i, the destination becomes (i + nums[i]) % n, where n is the array length. If the result is negative, add n to normalize it into the valid range [0, n-1]. This works because circular movement in an array is equivalent to modular index wrapping. Once the destination index is known, copy the value from nums[dest] into the output array.
This technique is a classic simulation optimization. Instead of simulating each move, you compute the final state directly using arithmetic. The algorithm performs a single pass through the array, making it linear time while using a separate result array.
Recommended for interviews: The direct circular index calculation is what interviewers expect. Explaining the step-by-step simulation first shows you understand the mechanics of the problem. Converting it into a modular arithmetic formula demonstrates strong reasoning about circular arrays and produces the optimal O(n) solution.
We create a result array ans. For each index, we move right or left |nums[i]| steps based on whether nums[i] is positive or negative, calculate the landing index, and assign the value at that index to ans[i].
The time complexity is O(n) and the space complexity is O(n), where n is the length of the array nums.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Step-by-Step Simulation | O(n²) | O(n) | When first reasoning about the problem or demonstrating the literal movement logic |
| Direct Circular Index Calculation | O(n) | O(n) | General case and optimal solution using modular arithmetic for circular arrays |
Transformed Array | Cleanest Implementation | Leetcode 3379 | codestorywithMIK • codestorywithMIK • 5,742 views views
Watch 9 more video solutions →Practice Transformed Array with our built-in code editor and test cases.
Practice on FleetCode