[LeetCode] Transformed Array

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

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

func constructTransformedArray(nums []int) []int {
n := len(nums)
res := make([]int, 0, n)

for i := 0; i < n; i++ {
idx := (i + nums[i]%n + n) % n
res = append(res, nums[idx])
}

return res
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/12/08/PS/LeetCode/transformed-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.