LeetCode 3379: Transformed Array Explained with Examples

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 jump steps:
    • jump > 0 โ†’ move right
    • jump < 0 โ†’ move left
    • jump == 0 โ†’ you โ€œlandโ€ on the same index, and result is 0 (which is already nums[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:

  1. Create an answer array result of length n
  2. For each index i:
    • compute target = (i + nums[i]) % n
    • set result[i] = nums[target]

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]