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

Published: (February 5, 2026 at 11:13 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

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

Problem Overview

You are given an integer array nums that should be treated as a circular array.
For each index i, move a distance equal to nums[i]:

  • Positive value → move right
  • Negative value → move left
  • Zero → stay in place

Create a new array result where result[i] is the value found after performing the move from index i.

Intuition: The Magic of Modulo

Wrapping around a circular array of size n is handled with the modulo operator (%).
A generic formula that works for both positive and negative jumps is:

target = ((i + nums[i]) % n + n) % n

The inner % n may produce a negative number; adding n and taking % n again converts it to a valid non‑negative index.

Walkthrough: Example

Input: nums = [3, -2, 1, 1] (n = 4)

inums[i]Calculationtarget indexresult[i]
03(0 + 3) % 4 = 331
1-2((1 - 2) % 4 + 4) % 4 = 331
21(2 + 1) % 4 = 331
31((3 + 1) % 4 + 4) % 4 = 003

Result: [1, 1, 1, 3]

Code Implementation

C++

class Solution {
public:
    vector constructTransformedArray(vector& nums) {
        int n = nums.size();
        vector result(n);
        for (int i = 0; i  List[int]:
        n = len(nums)
        result = [0] * n
        for i in range(n):
            target_index = (i + nums[i]) % n   # Python handles negative % correctly
            result[i] = nums[target_index]
        return result

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 be negative, so adjust
        let targetIndex = (i + (nums[i] % n) + n) % n;
        result[i] = nums[targetIndex];
    }
    return result;
};

Key Takeaways

  • Modular Arithmetic – The most efficient way to implement wrap‑around logic.
  • Language Nuances – Python’s % already yields a non‑negative result, while C++ and JavaScript need an extra + n adjustment.
  • Independent Operations – Each element is transformed independently, so a separate result array is required.

Final Thoughts

Understanding circular arrays is essential for many systems concepts, such as Round Robin Scheduling and Ring Buffers. Mastering the modulo operator simplifies a wide range of problems that involve wrap‑around behavior.

Back to Blog

Related posts

Read more »