VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-array-after-it-is-right-rotated-k-times/

⇱ Array after k Rotations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Array after k Rotations

Last Updated : 22 Jul, 2025

Given an array arr[] and an integer k, rotate the array in place k times to the right (clockwise). In each rotation, the last element moves to the front, and all other elements shift one position to the right. Modify the array in place, do not return anything.

Examples :

Input: arr[] = [1, 2, 3, 4, 5, 6], k = 2
Output: [5, 6, 1, 2, 3, 4]
Explanation:
=> We perform 2 right rotations (since k = 2):

  • After 1st rotation: Last element moves to front β†’ [6, 1, 2, 3, 4, 5]
  • After 2nd rotation: Again, last element to front β†’ [5, 6, 1, 2, 3, 4]

Input: arr[] = [1, 2, 3, 4, 5], k = 4
Output: [2, 3, 4, 5, 1]
Explanation:
=> We rotate the array 4 times to the right:

  • After 1st rotation: [5, 1, 2, 3, 4]
  • After 2nd rotation: [4, 5, 1, 2, 3]
  • After 3rd rotation: [3, 4, 5, 1, 2]
  • After 4th rotation: [2, 3, 4, 5, 1]

[Naive Approach] Using Recursion - O(n Γ— k) Time and O(k) Space

The main idea of this approach is to rotate the array to the right by one position, k times, using recursion. In each recursive call, the last element of the array is stored temporarily, all other elements are shifted one position to the right, and the saved element is placed at the front. This operation effectively performs a single right rotation. The function then calls itself with k - 1 until k reaches zero, at which point the recursion stops.


Output
5 6 1 2 3 4 

[Better Approach] Using Extra Space - O(n) Time and O(n) Space

The main idea is to first ensures that k is within bounds by taking k % n, where n is the array size. Then, we create a temporary result array where the last kelements of the original array are placed at the beginning. The remaining n - k elements are then copied after these. Finally, we copy all elements from the result array back to the original array to achieve an in-place update. This method uses index arithmetic to calculate positions efficiently.


Output
5 6 1 2 3 4 

[Expected Approach] Using Reversal Algorithm - O(n) Time and O(1)

Observing the Pattern:

Let’s take the example: Input: arr[] = [1, 2, 3, 4, 5, 6], k = 2
After rotating the array to the right by 2 positions, we get:
Output: [5, 6, 1, 2, 3, 4]
Observation: The last k elements (5, 6) move to the front in the same order, while the first n - k elements (1, 2, 3, 4) shift right.

How to Achieve This Efficiently

Instead of rotating one element at a time (which is slow), we use the Reversal Algorithm, which performs the rotation in-place in just 3 simple steps:

  • Reverse the last k elements
    β†’ Reverse the subarray from index n - k to n - 1
    β†’ [1, 2, 3, 4, 6, 5]
  • Reverse the first n - k elements
    β†’ Reverse the subarray from index 0 to n - k - 1
    β†’ [4, 3, 2, 1, 6, 5]
  • Reverse the entire array
    β†’ Reverse the full array from index 0 to n - 1
    β†’ [5, 6, 1, 2, 3, 4]

Working:

πŸ‘ DSA-1

Output
5 6 1 2 3 4 
Comment
Article Tags:
Article Tags: