Given an array arr[] of size N and D index, the task is to rotate the array by the D index.
Left Rotate: Array rotate by D element from left
Example:
Input:
arr[] = {1, 2, 3, 4, 5}
D = 2
Output:
3 4 5 1 2
Explanation: The intial array [1, 2, 3, 4, 5]
rotate by first index [2, 3, 4, 5, 1]
rotate by second index [3, 4, 5, 1, 2]Input:
arr[] = {10, 34, 56, 23, 78, 12, 13, 65}
D = 7
Output:
65 10 34 56 23 78 12 13
- Using temp array
Approach: In this method simply create a temporary array and copy the elements of the array arr[] from 0 to the Dth index. After that move, the rest elements of the array arr[] from index D to N. Then move the temporary array elements to the original array.
Input arr[] = [1, 2, 3, 4, 5], D = 2
1) Store the first d elements in a temp array: temp[] = [1, 2]
2) Shift rest of the arr[]: arr[] = [3, 4, 5]
3) Store back the D elements: arr[] = [3, 4, 5, 1, 2]Below is the implementation of the above approach :
// Java program to left rotate// an array by D elementsclassGFG {// Function to left rotate arr[]// of size N by DvoidleftRotate(intarr[],intd,intn){// create temp array of size dinttemp[] =newint[d];// copy first d element in array tempfor(inti =0; i < d; i++)temp[i] = arr[i];// move the rest element to index// zero to N-dfor(inti = d; i < n; i++) {arr[i - d] = arr[i];}// copy the temp array element// in origninal arrayfor(inti =0; i < d; i++) {arr[i + n - d] = temp[i];}}// utility function to print an arrayvoidprintArray(intarr[],intn){for(inti =0; i < n; i++)System.out.print(arr[i] +" ");}// Driver program to test above functionspublicstaticvoidmain(String[] args){GFG rotate =newGFG();intarr[] = {1,2,3,4,5};rotate.leftRotate(arr,2, arr.length);rotate.printArray(arr, arr.length);}}chevron_rightfilter_noneOutput:3 4 5 1 2
Time complexity: O(N)
Auxiliary Space: O(D) -
Rotate one by one:
Approach: Rotate the array recursively one by one element-Input arr[] = [1, 2, 3, 4, 5], D = 2
1) swap arr[0] to arr[1]
2) swap arr[1] to arr[2]
.
.
.
3) swap arr[N-1] to arr[N]
4) Repeat 1, 2, 3 to D timesTo rotate by one, store arr[0] in a temporary variable temp, move arr[1] to arr[0], arr[2] to arr[1] …and finally temp to arr[n-1]
Let us take the same example arr[] = [1, 2, 3, 4, 5], d = 2
Rotate arr[] by one 2 times
We get [2, 3, 4, 5, 1] after first rotation and [ 3, 4, 5, 1, 2] after second rotation.Below is the implementation of the above approach :
// Java program to left rotate// an array by d elementsclassGFG {// Function to left rotate arr[]// of size n by dvoidleftRotate(intarr[],intd,intn){for(inti =0; i < d; i++)leftRotatebyOne(arr, n);}voidleftRotatebyOne(intarr[],intn){inti, temp;temp = arr[0];for(i =0; i < n -1; i++)arr[i] = arr[i +1];arr[i] = temp;}// utility function to print an arrayvoidprintArray(intarr[],intn){for(inti =0; i < n; i++)System.out.print(arr[i] +" ");}// Driver program to test above functionspublicstaticvoidmain(String[] args){GFG rotate =newGFG();intarr[] = {1,2,3,4,5};rotate.leftRotate(arr,2, arr.length);rotate.printArray(arr, arr.length);}}chevron_rightfilter_noneOutput:3 4 5 1 2
Time complexity: O(N * D)
Auxiliary Space: O(1) -
A Juggling Algorithm:
Approach: This is an extension of method 2. Instead of moving one by one, divide the array into different sets where the number of sets is equal to GCD of n and d and move the elements within sets.If GCD is 1 as-is for the above example array (n = 5 and d = 2), then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
Below is the implementation of the above approach :
// Java program to left rotate// an array by d elementsclassGFG {// Function to left rotate arr[]// of siz N by DvoidleftRotate(intarr[],intd,intn){// To handle if d >= nd = d % n;inti, j, k, temp;intg_c_d = gcd(d, n);for(i =0; i < g_c_d; i++) {// move i-th values of blockstemp = arr[i];j = i;while(true) {k = j + d;if(k >= n)k = k - n;if(k == i)break;arr[j] = arr[k];j = k;}arr[j] = temp;}}// function to print an arrayvoidprintArray(intarr[],intsize){inti;for(i =0; i < size; i++)System.out.print(arr[i] +" ");}// Function to get gcd of a and bintgcd(inta,intb){if(b ==0)returna;elsereturngcd(b, a % b);}// Driver program to test above functionspublicstaticvoidmain(String[] args){GFG rotate =newGFG();intarr[] = {1,2,3,4,5};rotate.leftRotate(arr,2, arr.length);rotate.printArray(arr, arr.length);}}chevron_rightfilter_noneOutput:3 4 5 1 2
Time complexity: O(n)
Auxiliary Space: O(1)
Right Rotate:Array rotate by D element from Right
Example:
Input:
arr[] = {1, 2, 3, 4, 5}
D = 2
Output:
4 5 1 2 3
Explanation:
The intial array [1, 2, 3, 4, 5]
rotate first index [2, 3, 4, 5, 1]
rotate second index [3, 4, 5, 1, 2]
rotate third index [4, 5, 1, 2, 3]Input:
arr[] = {10, 34, 56, 23, 78, 12, 13, 65}
D = 5
Output:
56 23 78 12 13 65 10 34
- Using temp array
Approach: In this method simply create a temporary array and copy the elements of the array arr[] from 0 to the N – D index. After that move, the rest elements of the array arr[] from index D to N. Then move the temporary array elements to the original array.
Input arr[] = [1, 2, 3, 4, 5], D = 2 1) Store the first d elements in a temp array temp[] = [1, 2, 3] 2) Shift rest of the arr[] arr[] = [4, 5] 3) Store back the D elements arr[] = [4, 5, 1, 2, 3]
Below is the implementation of the above approach :
// Java program to rotate an array by// D elementsclassGFG {// Function to right rotate arr[]// of size N by DvoidrightRotate(intarr[],intd,intn){// create temp array of size dinttemp[] =newint[n - d];// copy first N-D element in array tempfor(inti =0; i < n - d; i++)temp[i] = arr[i];// move the rest element to index// zero to Dfor(inti = n - d; i < n; i++) {arr[i - d -1] = arr[i];}// copy the temp array element// in origninal arrayfor(inti =0; i < n - d; i++) {arr[i + d] = temp[i];}}// utility function to print an arrayvoidprintArray(intarr[],intn){for(inti =0; i < n; i++)System.out.print(arr[i] +" ");}// Driver program to test above functionspublicstaticvoidmain(String[] args){GFG rotate =newGFG();intarr[] = {1,2,3,4,5};rotate.rightRotate(arr,2, arr.length);rotate.printArray(arr, arr.length);}}chevron_rightfilter_noneOutput:4 5 1 2 3
Time complexity: O(N)
Auxiliary Space: O(D) -
Rotate one by one:
Approach: Rotate the array recursively one by one element-Input arr[] = [1, 2, 3, 4, 5], D = 2
1) swap arr[N] to arr[N-1]
2) swap arr[N-1] to arr[N-2]
.
.
.
3) swap arr[2] to arr[1]
4) Repeat 1, 2, 3 to D timesTo rotate by one, store arr[N] in a temporary variable temp, move arr[N-1] to arr[N], arr[N-2] to arr[N-1] … and finally temp to arr[1]
Let us take the same example arr[] = [1, 2, 3, 4, 5], d = 2
Rotate arr[] by one 2 times
We get [5, 1, 2, 3, 4] after first rotation and [ 4, 5, 1, 2, 3] after second rotation.Below is the implementation of the above approach :
// Java program to rotate an array by// d elementsclassGFG {// Function to right rotate arr[]// of size n by dvoidrightRotate(intarr[],intd,intn){for(inti = n; i > d; i--)rightRotatebyOne(arr, n);}voidrightRotatebyOne(intarr[],intn){inti, temp;temp = arr[0];for(i =0; i < n -1; i++)arr[i] = arr[i +1];arr[i] = temp;}// utility function to print an arrayvoidprintArray(intarr[],intn){for(inti =0; i < n; i++)System.out.print(arr[i] +" ");}// Driver program to test above functionspublicstaticvoidmain(String[] args){GFG rotate =newGFG();intarr[] = {1,2,3,4,5};rotate.rightRotate(arr,2, arr.length);rotate.printArray(arr, arr.length);}}chevron_rightfilter_noneOutput:4 5 1 2 3
Time complexity: O(N * D)
Auxiliary Space: O(1) -
A Juggling Algorithm:
Approach: This is an extension of method 2. Instead of moving one by one, divide the array into different sets where the number of sets is equal to GCD of n and d and move the elements within sets.If GCD is 1 as-is for the above example array (n = 5 and d =2), then elements will be moved within one set only, we just start with temp = arr[N] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
Below is the implementation of the above approach :
// Java program to rotate an array by// d elementsclassGFG {// Function to right rotate arr[]// of siz N by DvoidrightRotate(intarr[],intd,intn){// to use as left rotationd = n - d;d = d % n;inti, j, k, temp;intg_c_d = gcd(d, n);for(i =0; i < g_c_d; i++) {// move i-th values of blockstemp = arr[i];j = i;while(true) {k = j + d;if(k >= n)k = k - n;if(k == i)break;arr[j] = arr[k];j = k;}arr[j] = temp;}}// UTILITY FUNCTIONS// function to print an arrayvoidprintArray(intarr[],intsize){inti;for(i =0; i < size; i++)System.out.print(arr[i] +" ");}// Function to get gcd of a and bintgcd(inta,intb){if(b ==0)returna;elsereturngcd(b, a % b);}// Driver program to test above functionspublicstaticvoidmain(String[] args){GFG rotate =newGFG();intarr[] = {1,2,3,4,5};rotate.rightRotate(arr,2, arr.length);rotate.printArray(arr, arr.length);}}chevron_rightfilter_noneOutput:4 5 1 2 3
Time complexity: O(n)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Program to cyclically rotate an array by one
- Find an element in array such that sum of left array is equal to sum of right array
- Generate Array whose difference of each element with its left yields the given Array
- Check if an array can be Arranged in Left or Right Positioned Array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Left rotation of an array using vectors in C++
- Queries on Left and Right Circular shift on array
- Find the Mth element of the Array after K left rotations
- Remove all 1s from the adjacent left of 0s in a Binary Array
- Minimum element left from the array after performing given operations
- Print left rotation of array in O(n) time and O(1) space
- Find the index of the left pointer after possible moves in the array
- Quickly find multiple left rotations of an array | Set 1
- Closest greater or same value on left side for every element in array
- Find the nearest smaller numbers on left side in an array
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Reduce the array by deleting elements which are greater than all elements to its left
- Reorder an array such that sum of left half is not equal to sum of right half
- Count of array elements which are greater than all elements on its left
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

