↓Skip to main content
  1. LeetCode/

LeetCode 3379: Transformed Array

·1 min· ·
LeetCode Easy Array Simulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-02-05

  • Submissions: Accepted (first try)

Approach
#

Positive moves right and negative moves left by |nums[i]|, which together means landing at i + nums[i]. Wrap with % n for the circular array (Array Template 5); Python’s % gives 0..n-1 even for negatives, so no separate case is needed. nums[i] == 0 lands on itself and yields 0, also covered.

My Solution
#

class Solution:
    def constructTransformedArray(self, nums: List[int]) -> List[int]:
        n = len(nums)
        return [nums[(i + x) % n] for i, x in enumerate(nums)]

I originally split positive, negative and zero into three cases; same result.

Complexity
#

Time O(n), space O(n) for the output.

Mistakes to Avoid
#

  • In other languages % can be negative for negative operands; write ((i + x) % n + n) % n.

Interview Explanation
#

Moving either way lands at i + nums[i], taken mod n for the circular array; Python’s mod is non-negative even for negatives. Time O(n).

Related

LeetCode 3701: Compute Alternating Sum
·1 min
LeetCode Easy Array Simulation
LeetCode 3701 note: add even indices and subtract odd ones; with slices, sum(nums[::2]) - sum(nums[1::2])
LeetCode 3688: Bitwise OR of Even Numbers in an Array
·1 min
LeetCode Easy Bit-Manipulation Array Simulation
LeetCode 3688 note: OR only the even numbers; the starting value 0 is also the answer when there are none
LeetCode 1920: Build Array from Permutation
·1 min
LeetCode Easy Array Simulation
LeetCode 1920 note: follow the definition ans[i] = nums[nums[i]]; for O(1) extra space, stack the new value times n on top of the old one
LeetCode 3069: Distribute Elements Into Two Arrays I
·1 min
LeetCode Easy Array Two-Pointers Simulation
LeetCode 3069 note: simulate the rule: the first two go to separate arrays, then each element joins the array whose last element is larger
LeetCode 682: Baseball Game
·1 min
LeetCode Easy Array Stack Simulation
LeetCode 682 note: keep valid scores on a stack; C removes the last, D adds its double, and + adds the sum of the last two
LeetCode 3507: Minimum Pair Removal to Sort Array I
·2 mins
LeetCode Easy Simulation Array
LeetCode 3507 note: n is tiny, so simulate; the right side of a slice assignment must be a list