Transformed Array - 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 indexiand movenums[i]steps to the right in the circular array. Setresult[i]to the value of the index where you land. - If
nums[i] < 0: Start at indexiand moveabs(nums[i])steps to the left in the circular array. Setresult[i]to the value of the index where you land. - If
nums[i] == 0: Setresult[i]tonums[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.
Input & Output
Example 1 — Basic Circular Movement
$
Input:
nums = [3,1,0,-1]
›
Output:
[-1,1,0,3]
💡 Note:
Index 0: nums[0]=3, move 3 steps right: 0→1→2→3, result[0]=nums[3]=-1. Index 1: nums[1]=1, move 1 step right: 1→2, result[1]=nums[2]=0. Index 2: nums[2]=0, result[2]=0. Index 3: nums[3]=-1, move 1 step left: 3→2, result[3]=nums[2]=0.
Example 2 — Wrapping Around
$
Input:
nums = [1,2,-1,0]
›
Output:
[2,-1,1,0]
💡 Note:
Index 0: move 1 right to position 1, get nums[1]=2. Index 1: move 2 right: 1→2→3, get nums[3]=0. Index 2: move 1 left: 2→1, get nums[1]=2. Index 3: nums[3]=0, stays 0.
Example 3 — Large Steps with Wrapping
$
Input:
nums = [5,-3,2]
›
Output:
[2,5,-3]
💡 Note:
Index 0: move 5 right: 0→1→2→0→1→2, lands at 2, get nums[2]=2. Index 1: move 3 left: 1→0→2→1, lands at 1, get nums[1]=-3. Index 2: move 2 right: 2→0→1, get nums[1]=-3.
Constraints
- 1 ≤ nums.length ≤ 1000
- -1000 ≤ nums[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with positive (right), negative (left), and zero (stay) values
2
Process
Move in circular fashion based on each element's value
3
Output
New array with values from landing positions
Key Takeaway
🎯 Key Insight: Use modular arithmetic to handle circular array navigation efficiently without step-by-step counting
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code