Skip to main content

3379.js

/** ------------------------------------------------------------------------------
*
* 3379. Transformed Array
* Topics: Array
* https://leetcode.com/problems/transformed-array/?envType=daily-question&envId=2026-02-05
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} nums
* @return {number[]}
*/
var constructTransformedArray = function (nums) {
const n = nums.length

const answer = []
for (let i = 0; i < n; i++) {
const num = nums[i]

if (num === 0) {
answer.push(0)
} else {
const target = (((i + num) % n) + n) % n
answer.push(nums[target])
}
}
return answer
}

console.log(constructTransformedArray([1, 2, 3, 4, 5]))
console.log(constructTransformedArray([3, -2, 1, 1]))