← Back

Rotate an array by a given number of steps.

Given an array of integers and a number of steps, rotate the array to the right by that many steps.

Algorithm

  1. Calculate the effective rotation steps as steps % array.length.
  2. Reverse the entire array.
  3. Reverse the first k elements.
  4. Reverse the remaining elements.

Time Complexity

The time complexity of this algorithm is O(n), where n is the number of elements in the array.