Skip to main content

Transformed Array - Solution & Explanation

EasyArraySimulation6 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

Problem Statement

You are given an integer array nums that represents a circular array. Your task is to create a new array result of the same size, following these rules:

For each index i (where 0 <= i < nums.length), perform the following independent actions:
  • If nums[i] > 0: Start at index i and move nums[i] steps to the right in the circular array. Set result[i] to the value at the index where you land.
  • If nums[i] < 0: Start at index i and move abs(nums[i]) steps to the left in the circular array. Set result[i] to the value at the index where you land.
  • If nums[i] == 0: Set result[i] to nums[i].

Return the new array result.

Note: Since nums is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end.

 

Example 1:

Input: nums = [3,-2,1,1]

Output: [1,1,1,3]

Explanation:

  • For nums[0] that is equal to 3, If we move 3 steps to right, we reach nums[3]. So result[0] should be 1.
  • For nums[1] that is equal to -2, If we move 2 steps to left, we reach nums[3]. So result[1] should be 1.
  • For nums[2] that is equal to 1, If we move 1 step to right, we reach nums[3]. So result[2] should be 1.
  • For nums[3] that is equal to 1, If we move 1 step to right, we reach nums[0]. So result[3] should be 3.

Example 2:

Input: nums = [-1,4,-1]

Output: [-1,-1,4]

Explanation:

  • For nums[0] that is equal to -1, If we move 1 step to left, we reach nums[2]. So result[0] should be -1.
  • For nums[1] that is equal to 4, If we move 4 steps to right, we reach nums[2]. So result[1] should be -1.
  • For nums[2] that is equal to -1, If we move 1 step to left, we reach nums[1]. So result[2] should be 4.

 

Constraints:

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Approach Overview

Problem Overview: You are given an integer array nums. For each index i, move nums[i] steps in the array (right for positive values, left for negative values) using circular indexing. The value at the destination index becomes result[i]. The task is to build this transformed array.

Approach 1: Step-by-Step Simulation (O(n²) time, O(n) space)

The most direct approach simulates movement one step at a time. For every index i, repeatedly move left or right depending on the sign of nums[i]. Since the array is circular, wrap around when the pointer moves past either end. After completing the required number of steps, record the element at the final index. This method mirrors the problem statement closely and is easy to reason about, but repeated stepping can make the total work quadratic in the worst case.

Approach 2: Direct Circular Index Calculation (O(n) time, O(n) space)

You can compute the final position directly using modular arithmetic instead of moving step by step. For index i, the destination becomes (i + nums[i]) % n, where n is the array length. If the result is negative, add n to normalize it into the valid range [0, n-1]. This works because circular movement in an array is equivalent to modular index wrapping. Once the destination index is known, copy the value from nums[dest] into the output array.

This technique is a classic simulation optimization. Instead of simulating each move, you compute the final state directly using arithmetic. The algorithm performs a single pass through the array, making it linear time while using a separate result array.

Recommended for interviews: The direct circular index calculation is what interviewers expect. Explaining the step-by-step simulation first shows you understand the mechanics of the problem. Converting it into a modular arithmetic formula demonstrates strong reasoning about circular arrays and produces the optimal O(n) solution.

Solution

We create a result array ans. For each index, we move right or left |nums[i]| steps based on whether nums[i] is positive or negative, calculate the landing index, and assign the value at that index to ans[i].

The time complexity is O(n) and the space complexity is O(n), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Step-by-Step SimulationO(n²)O(n)When first reasoning about the problem or demonstrating the literal movement logic
Direct Circular Index CalculationO(n)O(n)General case and optimal solution using modular arithmetic for circular arrays

Video Solution

Transformed Array | Cleanest Implementation | Leetcode 3379 | codestorywithMIKcodestorywithMIK5,885 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Transformed Array easy or hard?
Transformed Array is classified as an Easy problem. The main idea is recognizing that circular movement can be simplified with modular arithmetic. Once that insight is clear, the implementation becomes a straightforward single-pass simulation.
Transformed Array Python/Java solution
The Python or Java solution iterates through the array and computes the destination index using modulo arithmetic. After adjusting negative indices, the value at that position is copied into a result array. The implementation is short and runs in O(n) time with O(n) extra space.
How to solve Transformed Array in O(n)?
Iterate through the array once and compute the circular destination index with (i + nums[i]) % n. If the modulo result is negative, add the array length n to normalize it. Copy the value from nums[dest] into the result array. This avoids step-by-step simulation and keeps the runtime linear.
What is the best approach for Transformed Array?
The best approach computes the final destination index using modular arithmetic. For each index i, calculate (i + nums[i]) % n to simulate circular movement in constant time. After adjusting negative results by adding n, you can directly read the value from that position. This gives an O(n) time and O(n) space solution.
Is Transformed Array asked at Google/Amazon/Meta?
Problems like Transformed Array appear frequently in interviews because they test reasoning about circular arrays and modular arithmetic. Variations of circular index movement and simulation problems are common in companies such as Amazon and Google.
What data structure is used in Transformed Array?
The problem primarily uses arrays and simple simulation logic. Modular arithmetic is used to wrap indices in a circular manner. No advanced data structures like heaps or trees are required.
What is the time complexity of Transformed Array?
The optimal solution runs in O(n) time because each element is processed once and the destination index is computed using constant-time arithmetic. A naive simulation that moves one step at a time can take O(n²) time in the worst case if elements require many moves.

Ready to solve this problem?

Practice Transformed Array with our built-in code editor and test cases.

Practice on FleetCode