Open In App

Array after k Rotations

Last Updated : 22 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

C++
#include <iostream>
#include <vector>
using namespace std;

void rotateclockwise(vector<int> &arr, int k) {
    if (k == 0) {
        return;
    }

    int n = arr.size();
    if (n == 0) return;

    // rotate array to the right by 1 position
    int temp = arr[n - 1];
    for (int i = n - 1; i > 0; i--) {
        arr[i] = arr[i - 1];
    }
    arr[0] = temp;

    // recursive call for remaining k-1 rotations
    rotateclockwise(arr, k - 1);
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5, 6};
    int k = 2;

    rotateclockwise(arr, k);
    for (auto it : arr) {
        cout << it << " ";
    }

    return 0;
}
Java
class GfG{

    public static void rotateclockwise(int[] arr, int k){
        if (k == 0 || arr.length == 0) {
            return;
        }

        int n = arr.length;

        // rotate right by one position
        int temp = arr[n - 1];
        for (int i = n - 1; i > 0; i--) {
            arr[i] = arr[i - 1];
        }
        arr[0] = temp;

        // recursive call for k - 1
        rotateclockwise(arr, k - 1);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        int k = 2;

        rotateclockwise(arr, k);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
Python
def rotateclockwise(arr, k):
    if k == 0:
        return

    n = len(arr)

    # rotate the array to the right by 
    # one position
    temp = arr[n - 1]
    for i in range(n - 1, 0, -1):
        arr[i] = arr[i - 1]
    arr[0] = temp

    # recursively rotate the remaining elements
    # k-1 times
    rotateclockwise(arr, k - 1)


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 6]
    k = 2
    rotateclockwise(arr, k)
    for it in arr:
        print(it, end=" ")
C#
using System;

class GfG{

    static void rotateclockwise(int[] arr, int k){
        
        if (k == 0 || arr.Length == 0) {
            return;
        }

        int n = arr.Length;

        // rotate the array to the right by 
        // one position
        int temp = arr[n - 1];
        for (int i = n - 1; i > 0; i--) {
            arr[i] = arr[i - 1];
        }
        arr[0] = temp;

        // recursively rotate the array k - 1 
        // times
        rotateclockwise(arr, k - 1);
    }

    static void Main() {
        int[] arr = { 1, 2, 3, 4, 5, 6};
        int k = 2;
        rotateclockwise(arr, k);
        foreach (int it in arr) {
            Console.Write(it + " ");
        }
    }
}
JavaScript
function rotateclockwise(arr, k) {
    
    if (k === 0 || arr.length === 0) {
        return;
    }

    let n = arr.length;

    // rotate the array to the right by 
    // one position
    let temp = arr[n - 1];
    for (let i = n - 1; i > 0; i--) {
        arr[i] = arr[i - 1];
    }
    arr[0] = temp;

    // recursive call for remaining k - 1 
    // rotations
    rotateclockwise(arr, k - 1);
}

// Driver Code
let arr = [1, 2, 3, 4, 5, 6];
let k = 2;
rotateclockwise(arr, k); 
console.log(arr.join(" "));

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 k elements 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.

C++
#include <iostream>
#include <vector>
using namespace std;
                    
void rotateclockwise(vector<int>& arr, int k) {

    int n = arr.size();

    // Handle cases where k > n
    k = k % n;

    // Temporary vector to store rotated elements
    vector<int> res;

    for (int i = 0; i < n; i++) {
        if (i < k) {
            // Add last k elements to front
            res.push_back(arr[n + i - k]);
        } 
        else {
            // Shift remaining elements
            res.push_back(arr[i - k]);
        }
    }

    // Copy rotated result back to original array
    for (int i = 0; i < n; i++) {
        arr[i] = res[i];
    }
}

int main() {
    
    vector<int> arr = {1, 2, 3, 4, 5, 6};
    int k = 2;
    
    rotateclockwise(arr, k);
    for (auto it : arr) {
        cout << it << " ";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    public static void rotateclockwise(int[] arr, int k) {
        int n = arr.length;

        // If rotation is greater than array size
        k = k % n;

        // Temporary array to store rotated elements
        int[] res = new int[n];

        for (int i = 0; i < n; i++) {
            if (i < k) {
                // Place last k elements at the beginning
                res[i] = arr[n + i - k];
            } 
            else {
                // Shift the rest
                res[i] = arr[i - k];
            }
        }

        // Copy back to original array (in-place update)
        for (int i = 0; i < n; i++) {
            arr[i] = res[i];
        }
    }

    public static void main(String[] args) {
        
        int[] arr = {1, 2, 3, 4, 5, 6};
        int k = 2;
        
        rotateclockwise(arr, k);
        for (int it : arr) {
            System.out.print(it + " ");
        }
    }
}
Python
def rotateclockwise(arr, k):
    n = len(arr)
    
    # If rotation count is greater than array size
    k = k % n

    # Temporary list to store rotated elements
    res = [0] * n

    for i in range(n):
        if i < k:
            # Place last k elements at front
            res[i] = arr[n + i - k]  
        else:
            # Shift remaining elements
            res[i] = arr[i - k]      

    # Copy result back to original array (in-place update)
    for i in range(n):
        arr[i] = res[i]

# Driver code
if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 6]
    k = 2
    rotateclockwise(arr, k)
    for it in arr:
        print(it, end=" ")
C#
using System;

class GfG{
    
    static void rotateclockwise(int[] arr, int k){
        
        int n = arr.Length;
        k = k % n;

        // Create a temporary array to store result
        int[] temp = new int[n];

        for (int i = 0; i < n; i++){
            
            // Calculate new position after rotation
            int newIndex = (i + k) % n;
            temp[newIndex] = arr[i];
        }

        // Copy back to original array (in-place update)
        for (int i = 0; i < n; i++){
            
            arr[i] = temp[i];
        }
    }

    static void Main(){
        
        int[] arr = { 1, 2, 3, 4, 5, 6 };
        int k = 2;
        
        rotateclockwise(arr, k);
        foreach (int it in arr){
            
            Console.Write(it + " ");
        }
    }
}
JavaScript
function rotateClockwise(arr, k) {
    const n = arr.length;

    // Handle cases where k > n
    k = k % n;

    // Temporary array to store rotated elements
    let res = [];

    for (let i = 0; i < n; i++) {
        if (i < k) {
            
            // Add last k elements to the front
            res.push(arr[n + i - k]);
        } 
        else {
            
            // Shift remaining elements
            res.push(arr[i - k]);
        }
    }

    // Copy rotated result back to original array
    for (let i = 0; i < n; i++) {
        arr[i] = res[i];
    }
}

// Driver Code
let arr = [1, 2, 3, 4, 5, 6];
let k = 2;
rotateclockwise(arr, k);
console.log(arr.join(" "));

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
C++
#include<iostream>
#include<vector>
using namespace std;

void rotateclockwise(vector<int>& arr, int k){
    int n = arr.size();
    if (n == 0) return;

    k = k % n;
    int i, j;

    // reverse last k elements
    for (i = n - k, j = n - 1; i < j; i++, j--){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // reverse first n - k elements
    for (i = 0, j = n - k - 1; i < j; i++, j--){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // reverse the entire array
    for (i = 0, j = n - 1; i < j; i++, j--){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5, 6};
    int k = 2;

    rotateclockwise(arr, k); 
    for (int i = 0; i < arr.size(); i++){
        cout << arr[i] << " ";
    }

    return 0;
}
Java
public class Main {

    public static void rotateclockwise(int[] arr, int k){
        int n = arr.length;
        if (n == 0) return;

        k = k % n;
        int i, j;

        // reverse last k elements
        for (i = n - k, j = n - 1; i < j; i++, j--){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        // reverse first n-k elements
        for (i = 0, j = n - k - 1; i < j; i++, j--){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        // reverse the entire array
        for (i = 0, j = n - 1; i < j; i++, j--){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        int k = 2; 
        rotateclockwise(arr, k); 

        for (int it : arr) {
            System.out.print(it + " ");
        }
    }
}
Python
def rotateclockwise(arr, k):
    n = len(arr)
    if n == 0:
        return

    k = k % n

    # reverse last k elements
    arr[n - k:] = reversed(arr[n - k:])

    # reverse first n-k elements
    arr[:n - k] = reversed(arr[:n - k])

    # reverse the entire array
    arr[:] = reversed(arr)


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 6]
    k = 2
    rotateclockwise(arr, k)

    for it in arr:
        print(it, end=" ")
C#
using System;

class GfG{
    
    static void rotateclockwise(int[] arr, int k){
        
        int n = arr.Length;
        if (n == 0) return;

        k = k % n;
        int i, j;

        // reverse last k elements
        for (i = n - k, j = n - 1; i < j; i++, j--){
            
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        // reverse first n - k elements
        for (i = 0, j = n - k - 1; i < j; i++, j--){
            
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        // reverse the entire array
        for (i = 0, j = n - 1; i < j; i++, j--){
            
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    static void Main(){
        
        int[] arr = {1, 2, 3, 4, 5, 6};
        int k = 2; 

        rotateclockwise(arr, k);

        foreach (int it in arr){
            
            Console.Write(it + " ");
        }
    }
}
JavaScript
// helper function to revese subarray 
// from start to end inclusive
function reverse(arr, start, end) {
    while (start < end) {
        let temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

function rotateclockwise(arr, k) {
    const n = arr.length;
    if (n === 0) return;

    k = k % n;

    // reverse last k numbers
    reverse(arr, n - k, n - 1);

    // reverse the first n-k terms
    reverse(arr, 0, n - k - 1);

    // reverse the entire array
    reverse(arr, 0, n - 1);
}

// Driver Code
let arr = [1, 2, 3, 4, 5, 6];
let k = 2;
rotateclockwise(arr, k); 
console.log(arr.join(" "));

Output
5 6 1 2 3 4 

Article Tags :

Similar Reads