Skip to content

Commit 19b8f41

Browse files
authored
Create 3379-transformed-array.js
1 parent db023e5 commit 19b8f41

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

3379-transformed-array.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var constructTransformedArray = function(nums) {
6+
const n = nums.length;
7+
const res = new Array(n).fill(0);
8+
for (let i = 0; i < n; i++) {
9+
if (nums[i] === 0) {
10+
res[i] = nums[i];
11+
} else if (nums[i] > 0) {
12+
const ind = (i + nums[i]) % n;
13+
res[i] = nums[ind];
14+
} else {
15+
const neg = Math.abs(nums[i]) % n;
16+
const ind = (i - neg + n) % n;
17+
// console.log(ind);
18+
res[i] = nums[ind];
19+
}
20+
}
21+
return res;
22+
};

0 commit comments

Comments
 (0)