3379. Transformed Array LeetCode Solution

In this guide, you will get 3379. Transformed Array LeetCode Solution with the best time and space complexity. The solution to Transformed Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Transformed Array solution in C++
  4. Transformed Array solution in Java
  5. Transformed Array solution in Python
  6. Additional Resources
3379. Transformed Array LeetCode Solution image

Problem Statement of Transformed Array

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 0: Start at index i and move nums[i] steps to the right in the circular array. Set result[i] to the value of 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 of 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.

See also  2245. Maximum Trailing Zeros in a Cornered Path LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3379. Transformed Array LeetCode Solution in C++

class Solution {
 public:
  vector<int> constructTransformedArray(vector<int>& nums) {
    const int n = nums.size();
    vector<int> ans;

    for (int i = 0; i < n; ++i)
      ans.push_back(nums[(i + nums[i] % n + n) % n]);

    return ans;
  }
};
/* code provided by PROGIEZ */

3379. Transformed Array LeetCode Solution in Java

class Solution {
  public int[] constructTransformedArray(int[] nums) {
    final int n = nums.length;
    int[] ans = new int[n];

    for (int i = 0; i < n; ++i)
      ans[i] = nums[(i + nums[i] % n + n) % n];

    return ans;
  }
}
// code provided by PROGIEZ

3379. Transformed Array LeetCode Solution in Python

class Solution:
  def constructTransformedArray(self, nums: list[int]) -> list[int]:
    n = len(nums)
    return [nums[(i + num % n + n) % n]
            for i, num in enumerate(nums)]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.