Transformed Array — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Transformed Array
Detailed explanation and reference materials
Problem Overview

3379. Transformed Array

Difficulty: Easy
Topics: Arrays
Companies: Premium


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].

🔁 Circular Array Rule

  • Moving past the last element wraps around to the beginning.
  • Moving before the first element wraps around to the end.

Example 1

Input

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

Output

[1, 1, 1, 3]

Explanation

  • nums[0] = 3: move 3 steps right → reach nums[3] → result[0] = 1
  • nums[1] = -2: move 2 steps left → reach nums[3] → result[1] = 1
  • nums[2] = 1: move 1 step right → reach nums[3] → result[2] = 1
  • nums[3] = 1: move 1 step right → reach nums[0] → result[3] = 3

Example 2

Input

nums = [-1, 4, -1]

Output

[-1, -1, 4]

Explanation

  • nums[0] = -1: move 1 step left → reach nums[2] → result[0] = -1
  • nums[1] = 4: move 4 steps right → reach nums[2] → result[1] = -1
  • nums[2] = -1: move 1 step left → reach nums[1] → result[2] = 4

Constraints

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

import java.util.Arrays;

public class Main {

public static void main(String[] args) {
    // Hardcoded input
    int[] nums = {3, -2, 1, 1};

    int[] result = transformedArray(nums);

    // Print result
    System.out.println(Arrays.toString(result));
}

public static int[] transformedArray(int[] nums) {
    int n = nums.length;
    int[] result = new int[n];

    for (int i = 0; i < n; i++) {
        if (nums[i] == 0) {
            result[i] = 0;
        } else {
            int move = nums[i];
            int newIndex = (i + move) % n;

            // Handle negative index
            if (newIndex < 0) {
                newIndex += n;
            }

            result[i] = nums[newIndex];
        }
    }

    return result;
}

}

— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
arrays
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/transformed-array/description

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.