DEV Community

Cover image for 🎫Beginner-Friendly Guide 'Transformed Array' - Problem 3379 (C++, Python, JavaScript)
Om Shree
Om Shree

Posted on

🎫Beginner-Friendly Guide 'Transformed Array' - Problem 3379 (C++, Python, JavaScript)

Navigating through arrays is a fundamental skill, but things get interesting when the edges disappear and the structure becomes circular. This problem challenges you to think about movement and indexing in a wrap-around environment, a concept used everywhere from game loops to network buffers.


You're given:

  • An integer array nums representing a circular array.
  • Specific movement rules based on whether a value is positive (move right), negative (move left), or zero (stay put).

Your goal:

  • Create a new array result where each element at index i is the value found after traveling the distance specified by nums[i] from that starting position.

Intuition: The Magic of Modulo

The core challenge here is "wrapping around." In a circular array of size , if you are at the last index and move one step right, you land back at index 0. Mathematically, we handle this using the modulo operator (%).

When we calculate a new index using (i + nums[i]) % n, we might encounter a negative result if we move to the left. To ensure we always land on a valid positive index, we can use the formula:

This formula works universally for both positive and negative jumps. It ensures that any negative "landing spot" is shifted forward by to find its equivalent positive position in the circle.


Walkthrough: Understanding the Examples

Let's look at Example 1: nums = [3, -2, 1, 1] where .

  1. Index 0 (nums[0] = 3): Move 3 steps right from 0. . We land at index 3. nums[3] is 1.
  2. Index 1 (nums[1] = -2): Move 2 steps left from 1. . To wrap it: . We land at index 3. nums[3] is 1.
  3. Index 2 (nums[2] = 1): Move 1 step right from 2. . We land at index 3. nums[3] is 1.
  4. Index 3 (nums[3] = 1): Move 1 step right from 3. . Wrap it: . We land at index 0. nums[0] is 3.

Result: [1, 1, 1, 3]


Code Implementation

C++

class Solution {
public:
    vector<int> constructTransformedArray(vector<int>& nums) {
        int n = nums.size();
        vector<int> result(n);
        for (int i = 0; i < n; i++) {
            // Calculate the target index using circular logic
            int targetIndex = (i + nums[i] % n + n) % n;
            result[i] = nums[targetIndex];
        }
        return result;
    }
};

Enter fullscreen mode Exit fullscreen mode

Python

class Solution:
    def constructTransformedArray(self, nums: List[int]) -> List[int]:
        n = len(nums)
        result = [0] * n
        for i in range(n):
            # Python's % operator handles negative numbers differently than C++,
            # making (i + nums[i]) % n naturally circular.
            target_index = (i + nums[i]) % n
            result[i] = nums[target_index]
        return result

Enter fullscreen mode Exit fullscreen mode

JavaScript

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var constructTransformedArray = function(nums) {
    const n = nums.length;
    const result = new Array(n);
    for (let i = 0; i < n; i++) {
        // JS modulo can return negative, so we add n before taking modulo again
        let targetIndex = (i + nums[i] % n + n) % n;
        result[i] = nums[targetIndex];
    }
    return result;
};

Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Modular Arithmetic: The most efficient way to handle "wrap-around" logic in programming.
  • Language Nuances: Note how Python's % operator automatically handles negative results, while C++ and JavaScript require adding to ensure a positive index.
  • Independent Operations: Because each transformation is independent, we must store results in a new array rather than modifying the original in-place.

Final Thoughts

Understanding circular arrays is a rite of passage for software engineers. This logic is the backbone of Round Robin Scheduling in operating systems and Ring Buffers in high-performance streaming data. Master the modulo operator today, and you will find it simplifies many complex problems in the future.

Top comments (0)