Problem 266784 · medium · Phase 02 Linear Data Structures

Apply a Sequence of Rotations

arrays · rotation · modular arithmetic

Given a list nums and a list of integers moves, apply the moves in order and return the resulting list as a new list. A positive move m rotates the list to the right by m positions; a negative move rotates it to the left by |m| positions; 0 does nothing.

Examples

Input:  nums = [1, 2, 3, 4, 5], moves = [1, -3, 1]
Output: [2, 3, 4, 5, 1]
Explanation: [1,2,3,4,5] -> [5,1,2,3,4] -> [3,4,5,1,2] -> [2,3,4,5,1].

Input:  nums = [1, 2, 3, 4], moves = [4, -8, 12]
Output: [1, 2, 3, 4]

Constraints

  • 0 <= len(nums) <= 10**5, 0 <= len(moves) <= 10**5
  • -10**9 <= moves[i] <= 10**9
  • Return a new list; do not modify nums.
  • Target: O(len(nums) + len(moves)) time; rotating once per move is too slow.

Goals

  • Collapse many rotations into one
  • Handle negative and oversized rotation amounts with modulo
  • Avoid touching the list once per move
Starting Python…