# Array Rotation

## Rotation of an array at the given Index

This page has the solution to the program in which we have to rotate the elements of an array over their indices. For example an array is entered Arr={1,2,3,4,5,6,7,8,9} and it is asked to rotate this array to its left by 2 positions then the Output will be Arr={3,4,5,6,7,8,9,1,2} or to its right by 2 positions then the output will be Arr={8,9,1,2,3,4,5,6,7}. ## Steps for Array Rotation in JAVA

There are following Step of Array Rotation

• Take a Temporary variable in which we’ll store the first element of the array if the rotation is in left direction or the last element in case of right rotation.
• One by one store the elements in their left/right adjacent indices accordingly
• Now store the element in the temporary variable back to the last or first index. ## Algorithm for Array Rotation

• Rotate(int [] arr, int n)
• FOR i from 1 to pivot index:
• temp= last element/initial element
• FOR i=0 to i=arr.length
• arr[j]=arr[j-1]/arr[j+1]
• END of FOR
• arr / arr[arr.length} = temporary
• END of FOR

## Program for Array Rotation in JAVA

```import java.util.*;
class Main
{
void rightRotate(int[] arr, int n)
{
int temp;
for (int i = 1; i <= n; i++)             {             temp = arr[arr.length-1];             for (int j = arr.length-1; j > 0; j--)
{
arr[j] = arr[j-1];
}
arr = temp;
}
System.out.println("Input Array After Right Rotation By "+n+" Positions :");
System.out.println(Arrays.toString(arr));
}

void leftRotate(int[] arr, int n)
{
int temp;
for (int i = 0; i < n; i++)
{
temp = arr;
for (int j = 0; j < arr.length-1; j++)
{
arr[j] = arr[j+1];
}
arr[arr.length - 1] = temp;
}
System.out.println("Input Array After Left Rotation By "+n+" Positions :");
System.out.println(Arrays.toString(arr));
}

public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the length of the array you wish to rotate: ");
int l=s.nextInt();
int arr1[]=new int[l];
int arr2[]=new int[l];
System.out.println("Enter the Elements");
for(int i=0 ; i < l; i++)
{
arr1[i]=s.nextInt();
arr2[i]=arr1[i];
}
System.out.println("Enter the index of rotation");
int r=s.nextInt();
System.out.println("Input Array Before Rotation :");
System.out.println(Arrays.toString(arr1));
Main m= new Main();
m.rightRotate(arr1, r);
m.leftRotate(arr2, r);

}
}```
```Enter the length of the array you wish to rotate:
7
Enter the Elements
1
2
3
4
5
6
7
Enter the index of rotation
3
Input Array Before Rotation :
[1, 2, 3, 4, 5, 6, 7]
Input Array After Right Rotation By 3 Positions :
[5, 6, 7, 1, 2, 3, 4]
Input Array After Left Rotation By 3 Positions :
[4, 5, 6, 7, 1, 2, 3]
```

### One comment on “Array Rotation”

• ajit

package com.company1;
import java.util.Arrays;
import java.util.Scanner;

public class Main
{

public static int[] rotation(int[] arr,int k)
{
int j = 0;

while(k>0)
{
int temp = arr[arr.length-1];

for(j=arr.length-1;j>0;j–)
{

arr[j] = arr[j-1];

}

arr[j] = temp;
k–;

}

return arr;
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(“how many numbers you want to put in arrays ?”);
int number = sc.nextInt();
System.out.println(“now print those ” + number + ” arrays :”);

int[] originalArray = new int[number];

for(int i=0;i<originalArray.length;i++)
{
int values = sc.nextInt();
originalArray[i] = values;

}

int[] duplicateArray = new int[originalArray.length];
duplicateArray = Arrays.copyOf(originalArray,originalArray.length);

System.out.println("now everything is set ,just tell me to how many places you have to right rotate your arrays?");
int k = sc.nextInt();

int[] printArray = rotation(duplicateArray,k);

System.out.println("Here is your original array :");
for(int i=0;i<originalArray.length;i++)
{
System.out.print(" " + originalArray[i]);
}

System.out.println(); 0