Editorial
Solution walkthrough
Review the explanation, then compare the reference implementation.
Video explanation
Solution
Intuition
Moving right by a positive value or left by a negative value is the same signed index shift. Circular wrapping is handled by taking that shifted index modulo the array length. A zero shift naturally stays at the current position.
Approach
For every index i with value v, compute the circular destination corresponding to i + v. Normalize it into the range from zero through n - 1, then append the original array's value at that destination to the result.
Return the separate result array after processing all positions. Reading from the unchanged input is important: every destination is defined using the original values, not previously transformed entries. Modular indexing also handles shifts larger than one full revolution without simulating individual steps.
class Solution:
def constructTransformedArray(self, nums: List[int]) -> List[int]:
n = len(nums)
res = []
for i, v in enumerate(nums):
res.append(nums[(i + v) % n])
return resComplexity analysis
Let n = nums.length. Time: O(n). Auxiliary space: O(1) excluding the O(n) returned array.
