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 at 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 at 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
Inputnums = [3,-2,1,1]
Output[1,1,1,3]
Moving from each index according to its value gives landing values 1, 1, 1, and 3.
Example 2
Inputnums = [-1,4,-1]
Output[-1,-1,4]
Moving from each index according to its value gives landing values -1, -1, and 4.

Constraints

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate