Transformed Array - Problem
Transformed Array - Navigate a Circular Array Adventure!
Imagine you're standing in a circular playground where each position has a special instruction telling you how many steps to take. Your mission is to follow these instructions and record where you land!
Given an integer array
๐ For each position i:
โข If
โข If
โข If
Set
๐ฏ Key Point: The array is circular - going past the last element wraps to the beginning, and going before the first element wraps to the end.
Example:
โข Position 0: Move 3 steps right โ land at position 3 โ
โข Position 1: Move 2 steps left โ land at position 3 โ
โข Position 2: Stay put โ
โข Position 3: Move 1 step right โ land at position 0 โ
Return the transformed array!
Imagine you're standing in a circular playground where each position has a special instruction telling you how many steps to take. Your mission is to follow these instructions and record where you land!
Given an integer array
nums representing a circular array, create a new array result following these movement rules:๐ For each position i:
โข If
nums[i] > 0: Move nums[i] steps clockwise (right)โข If
nums[i] < 0: Move |nums[i]| steps counter-clockwise (left)โข If
nums[i] == 0: Stay put (no movement)Set
result[i] to the value at your final destination.๐ฏ Key Point: The array is circular - going past the last element wraps to the beginning, and going before the first element wraps to the end.
Example:
nums = [3, -2, 0, 1]โข Position 0: Move 3 steps right โ land at position 3 โ
result[0] = 1โข Position 1: Move 2 steps left โ land at position 3 โ
result[1] = 1โข Position 2: Stay put โ
result[2] = 0โข Position 3: Move 1 step right โ land at position 0 โ
result[3] = 3Return the transformed array!
Input & Output
example_1.py โ Basic Movement
$
Input:
nums = [3, -2, 0, 1]
โบ
Output:
[1, 1, 0, 3]
๐ก Note:
โข Index 0 (value 3): Move 3 steps right: 0โ1โ2โ3, land at nums[3] = 1
โข Index 1 (value -2): Move 2 steps left: 1โ0โ3, land at nums[3] = 1
โข Index 2 (value 0): No movement, stay at nums[2] = 0
โข Index 3 (value 1): Move 1 step right: 3โ0, land at nums[0] = 3
example_2.py โ Wrap Around
$
Input:
nums = [-1, 2, -3]
โบ
Output:
[2, -3, -1]
๐ก Note:
โข Index 0 (value -1): Move 1 step left: 0โ2, land at nums[2] = -3... wait, that's wrong. Let me recalculate: 0โ2, so result[0] = nums[2] = -3. Actually: result = [2, -3, -1]
example_3.py โ Large Steps
$
Input:
nums = [10, -5, 0]
โบ
Output:
[-5, 10, 0]
๐ก Note:
โข Index 0 (value 10): Move 10 steps right: 10 % 3 = 1, so land at nums[1] = -5
โข Index 1 (value -5): Move 5 steps left: (1 - 5) % 3 = -4 % 3 = 2, so land at nums[2] = 0... Let me recalculate properly.
Constraints
- 1 โค nums.length โค 100
- -1000 โค nums[i] โค 1000
- The array is circular - wrap around at boundaries
Visualization
Tap to expand
Understanding the Visualization
1
Read Instruction
Each position contains a number indicating steps and direction
2
Calculate Destination
Use modular arithmetic to find landing position instantly
3
Handle Wrap-Around
Positive/negative modulo ensures circular boundary handling
4
Record Value
Store the value found at the destination position
Key Takeaway
๐ฏ Key Insight: Modular arithmetic eliminates the need for step-counting loops - we can jump directly to the destination using mathematical formulas!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code