#3379
Transformed Array
newbie · 290 · lc easy +20 · 70.4% accepted · 448 likes · top 79%
Description
You are given an integer array nums representing a circular array. Build a new array result of the same length using these rules for each index i (independently):
- If nums[i] > 0: start at index i, move nums[i] positions to the right (circularly), and set result[i] to the element at the landing index.
- If nums[i] < 0: start at index i, move abs(nums[i]) positions to the left (circularly), and set result[i] to the element at the landing index.
- If nums[i] == 0: set result[i] to nums[i].
Return result.
Note: moving past the last element wraps to the front, and moving before the first element wraps to the back.
Code
1
2
3