Transformed Array (LeetCode 3379): Solution and Explanation
Treat the array as a ring. For each index, hop nums[i] steps (right if positive, left if negative) and record the value you land on.
Intuition
Walking k steps right around a ring of n cells from index i lands on (i + k) mod n. Walking k steps left lands on (i - k) mod n.
A negative nums[i] already means walking left, so both cases collapse into one formula: j = (i + nums[i]) mod n.
Zero is covered too. A hop of 0 lands on i itself, and nums[i] there is 0, which is exactly what the rule asks for.
Hops longer than the ring just run full laps first. The mod throws the laps away and keeps the leftover steps.
Approach: Modular index
- Let
n = len(nums)and makeresultof sizen. - For each
i, computej = ((i + nums[i]) % n + n) % nso the index is never negative. - Set
result[i] = nums[j]. - Return
result.
Step-by-step example
nums = [3,-2,1,1]
- i = 0: hop +3 to index 3, so result[0] = 1.
- i = 1: hop -2 to (1 - 2) mod 4 = 3, so result[1] = 1.
- i = 2: hop +1 to index 3, so result[2] = 1.
- i = 3: hop +1 to (3 + 1) mod 4 = 0, so result[3] = 3. Return [1,1,1,3].
Code: Python, Java, C++
1class Solution:
2 def constructTransformedArray(self, nums: List[int]) -> List[int]:
3 n = len(nums)
4 result = [0] * n
5 for i in range(n):
6 j = (i + nums[i]) % n
7 result[i] = nums[j]
8 return result1class Solution {
2 public int[] constructTransformedArray(int[] nums) {
3 int n = nums.length;
4 int[] result = new int[n];
5 for (int i = 0; i < n; i++) {
6 int j = ((i + nums[i]) % n + n) % n;
7 result[i] = nums[j];
8 }
9 return result;
10 }
11}1class Solution {
2public:
3 vector<int> constructTransformedArray(vector<int>& nums) {
4 int n = nums.size();
5 vector<int> result(n);
6 for (int i = 0; i < n; i++) {
7 int j = ((i + nums[i]) % n + n) % n;
8 result[i] = nums[j];
9 }
10 return result;
11 }
12};Time and space complexity
O(n)
Each index does one constant-time hop.
O(n)
Only the output array is allocated.
Other approaches
Step one cell at a time (time O(n * max|nums[i]|), space O(n))
Simulate each hop by moving one index per step with wraparound. It is correct but costs up to |nums[i]| steps per index.
Branch on the sign (time O(n), space O(n))
Handle positive, negative and zero values in three branches. It reads close to the statement, but the single mod formula covers all three.
Common mistakes
- Using
(i + nums[i]) % nin Java, C++, C#, JavaScript or Go. The remainder can be negative there, so add n and take the mod again. - Writing the answer back into nums. Later hops must read the original values.
- Assuming |nums[i]| < n. Values go up to 100 while n can be 1, so a hop can lap the ring many times.
- Special-casing zero with the wrong value. result[i] must be nums[i], which is 0.
Frequently asked questions
Why add n before the second mod?
In most languages -1 % 4 is -1, not 3. Adding n moves the remainder into 0..2n-1, and the second mod brings it into 0..n-1.
Does Python need the extra + n?
No. Python's % already returns a value with the sign of the divisor, so (i + nums[i]) % n is never negative.
Do the hops depend on each other?
No. Every result[i] reads from the original nums, so the order of the loop does not matter.