VOOZH about

URL: https://www.geeksforgeeks.org/dsa/array-rotation/

⇱ Rotate an Array by d - Counterclockwise or Left - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Rotate an Array by d - Counterclockwise or Left

Last Updated : 26 May, 2026

Given an array of integers arr[] of size n, the task is to rotate the array elements to the left by d positions.

Examples:

Input: arr[] = [1, 2, 3, 4, 5, 6], d = 2
Output: [3, 4, 5, 6, 1, 2]
Explanation: After first left rotation, arr[] becomes [2, 3, 4, 5, 6, 1] and after the second rotation, arr[] becomes [3, 4, 5, 6, 1, 2]

Input: arr[] = [1, 2, 3], d = 4
Output: [2, 3, 1]
Explanation: The array is rotated as follows:

  • After first left rotation, arr[] = [2, 3, 1]
  • After second left rotation, arr[] = [3, 1, 2]
  • After third left rotation, arr[] = [1, 2, 3]
  • After fourth left rotation, arr[] = [2, 3, 1]

[Naive Approach] Rotate One by One - O(n * d) Time and O(1) Space

In each iteration, shift the elements by one position to the left in a circular fashion (the first element becomes the last). Perform this operation d times to rotate the elements to the left by d positions.

Illustration:

Let us take arr[] = [1, 2, 3, 4, 5, 6]d = 2.

First Step:
        => Rotate to left by one position.
        => arr[] = [2, 3, 4, 5, 6, 1]
Second Step:
        => Rotate again to left by one position
        => arr[] = [3, 4, 5, 6, 1, 2[
Rotation is done 2 times.
So the array becomes arr[] = [3, 4, 5, 6, 1, 2]


Output
3 4 5 6 1 2 

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

This problem can be solved using the below idea:

The idea is to use a temporary array of size n, where n is the length of the original array. If we left rotate the array by d positions, the last n - d elements will be at the front and the first d elements will be at the end.

  • Copy the last (n - d)elements of original array into the first n - d positions of temporary array.
  • Then copy the first d elements of the original array to the end of temporary array.
  • Finally, copy all the elements of temporary array back into the original array.

Working:


Below is the implementation of the algorithm:


Output
3 4 5 6 1 2 

[Expected Approach 1] Juggling Algorithm - O(n) Time and O(1) Space

The idea is to use Juggling Algorithm which is based on rotating elements in cycles. Each cycle is independent and represents a group of elements that will shift among themselves during the rotation. If the starting index of a cycle is i, then next elements of the cycle can be found at indices (i + d) % n, (i + 2d) % n, (i + 3d) % n ... and so on till we return to the original index i.

So for any index i, we know that after rotation, the element that will occupy this position is arr[(i + d) % n]. Consequently, for every index in the cycle, we will place the element that should be in that position after the rotation is completed.

Please refer Juggling Algorithm for Array Rotation to know more about the implementation.

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

The idea is based on the observation that if we left rotate the array by d positions, the last (n - d) elements will be at the front and the first d elements will be at the end.

  • Reverse the subarray containing the first d elements of the array.
  • Reverse the subarray containing the last (n - d) elements of the array.
  • Finally, reverse all the elements of the array.

Working:


Below is the implementation of the algorithm:


Output
3 4 5 6 1 2 


Comment