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 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] = 3

Return 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
3Index 0-2Index 10Index 21Index 33 steps2 steps back
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!
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 15
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen