Learn
Exam

Rotating an Array

Difficulty
STARTER

Problem

Given an array numbers consisting of integer elements and an integer direction representing the rotation direction, write a function solution that returns the rotated array.

  • If direction is positive, rotate the array to the right, and if negative, rotate it to the left.
  • For example, if direction is -4, numbers is rotated 4 steps to the left. If direction is 4, numbers is rotated 4 steps to the right.

Constraints

  • Each element in numbers is an integer with an absolute value not exceeding 500.
  • numbers is not empty, and its length does not exceed 100.
  • The absolute value of direction is at least 1 and less than the length of numbers.

Test Case

Test Case Number numbers direction return
1 [1, 2, 3, 4] 1 [4, 1, 2, 3]
2 [1, 2, 3, 4] -2 [3, 4, 1, 2]
3 [1, 2, 3, 4] 3 [2, 3, 4, 1]

Explanation for Test Case 1

  • Rotating numbers 1 step to the right results in [4, 1, 2, 3].

Explanation for Test Case 2

  • Rotating numbers 2 steps to the left results in [3, 4, 1, 2].

Explanation for Test Case 3

  • Rotating numbers 3 steps to the right results in [2, 3, 4, 1].