Some LeetCode problems feel like math. 3379. Transformed Array feels more like a board game.
Every position in the array gives you an instruction:
- Positive number? Jump right
- Negative number? Jump left
- Zero? Stay put (and the result is 0)
Your job is to build a new array where each index i stores the value you land on after following nums[i] steps in a circular way.
What the problem is really asking
Youโre given an integer array nums that behaves like a circle (the end connects back to the beginning).
For each index i, you do this independently:
- Let
jump = nums[i] - Start at
i - Move
jumpsteps:jump > 0โ move rightjump < 0โ move leftjump == 0โ you โlandโ on the same index, and result is 0 (which is alreadynums[i])
- Put the value at your landing position into
result[i]
Important: You always read from the original nums, not from a partially updated array.
The key trick: circular movement = modulo
Circular arrays are basically screaming: use modulo.
If the array length is n, then the landing index is: target=(i+nums[i])โmodโn\text{target} = (i + nums[i]) \bmod ntarget=(i+nums[i])modn
That one line handles:
- wrapping past the end
- wrapping before the start
- big jumps (bigger than
n) - negative jumps (with one important language-specific caveat below)
Walkthrough example
Letโs use the common example:
nums = [3, -2, 1, 1]
Array length n = 4
i = 0
- nums[0] = 3 โ move 3 right
- target = (0 + 3) % 4 = 3
- result[0] = nums[3] = 1
i = 1
- nums[1] = -2 โ move 2 left
- target = (1 – 2) % 4 = -1 % 4 = 3
- result[1] = nums[3] = 1
i = 2
- nums[2] = 1 โ move 1 right
- target = (2 + 1) % 4 = 3
- result[2] = nums[3] = 1
i = 3
- nums[3] = 1 โ move 1 right
- target = (3 + 1) % 4 = 0
- result[3] = nums[0] = 3
โ Final result: [1, 1, 1, 3]
The clean O(n) solution idea
Instead of โmoving step-by-stepโ (like a simulation where you walk 1 step at a time), you can jump directly using math:
- Create an answer array
resultof lengthn - For each index
i:- compute
target = (i + nums[i]) % n - set
result[i] = nums[target]
- compute
Thatโs it.
Python solution (simple and readable)
Pythonโs % already returns a non-negative result, even for negativesโso this is super clean:
from typing import List
class Solution:
def constructTransformedArray(self, nums: List[int]) -> List[int]:
n = len(nums)
res = [0] * n
for i, jump in enumerate(nums):
target = (i + jump) % n
res[i] = nums[target]
return res
Why no special case for zero?
If jump == 0:
- target = (i + 0) % n = i
- res[i] = nums[i] = 0
That matches the requirement automatically.
If you code in C++/Java: watch out for negative modulo
In Python:
(-1) % 4 == 3โ
In many languages like C++/Java:
(-1) % 4 == -1โ (still negative)
So youโll often see a safe pattern like:
target = (i + (nums[i] % n) + n) % n;
That guarantees target ends up between 0 and n-1.
Common mistakes people make
1) Forgetting itโs circular
If you do i + nums[i] without wrapping, youโll crash with out-of-bounds.
2) Confusing โresult arrayโ with โin-place modificationโ
This is a new array. If you overwrite nums, later indices might use already-changed values (wrong).
3) Mishandling negative jumps (in C++/Java)
If your modulo can be negative, your index can be negative too.
Complexity
- Time: O(n) โ one pass
- Space: O(n) โ output array
With constraints up to length 100, even slower methods might pass, but this is the clean โinterview-readyโ approach.
Final takeaway
This problem is basically saying:
โAt each index, treat the value as a jump instruction, and read the number you land onโwrapping around like a circle.โ
Once you see that, the solution becomes a one-liner:res[i] = nums[(i + nums[i]) % n]