Rotate array in Java
In this post, you will learn how to rotate an array using the Java Programming Language.
Java Array Rotation
Array rotation means we need to rotate the elements of an array in the right or left direction by a given number of positions.
Types of Array Rotation
- Left Rotation
- Right Rotation
Array Left Rotation
An array is said to be left rotated if all elements of the array are moved to its left by one position, and the first element of the array will be added to the end of the list. One approach is to loop through the array by shifting each element of the array to its previous position. For example, if {1, 2, 3, 4, 5, 6, 7} is an input array, then rotating this array in the left direction by three positions will give {4, 5, 6, 7, 1, 2, 3}.
Array left rotation Java
In the given program, we have initialised an array and stored it in the arr variable. Next, we have defined one temporary variable, temp which holds the first element of the arr. First, we copy the first element of arr into temp and then shift all elements one position left. After shifting, copy back temp as the last element of arr. We have repeated this process n times.
import java.util.Arrays;
public class ArrayLeftRotation
{
private static void leftRotate(int[] arr, int n)
{
System.out.println("Array Before Rotation: ");
System.out.println(Arrays.toString(arr));
int temp;
for (int i = 0; i < n; i++)
{
temp = arr[0];
for (int j = 0; j < arr.length-1; j++)
{
arr[j] = arr[j+1];
}
arr[arr.length - 1] = temp;
}
System.out.println("Array after left rotation by "+n+" positions: ");
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args)
{
leftRotate(new int[] {1,2,3,4,5,6,7}, 3);
}
}
Output of the above code:
Array Before Rotation:
[1, 2, 3, 4, 5, 6, 7]
Array after left rotation by 3 positions:
[4, 5, 6, 7, 1, 2, 3]
Array Right Rotation
An array is said to be right rotated if all elements of the array are moved to its right by one position. One approach is to loop through the array by shifting each element of the array to its next position. For example, if {1, 2, 3, 4, 5, 6, 7} is an input array then rotating this array in the right direction by three positions will give {5, 6, 7, 1, 2, 3, 4}.
Array right rotation Java
In the given program, we have initialised an array and stored it in the arr variable. Next, we have defined one temporary variable, temp which holds the last element of the arr. First, we copy the last element of arr into temp and then shift all elements one position right. After shifting, copy back temp as the first element of arr. We have repeated this process n times.
import java.util.Arrays;
public class ArrayRightRotation
{
private static void rightRotate(int[] arr, int n)
{
System.out.println("Array before rotation: ");
System.out.println(Arrays.toString(arr));
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[0] = temp;
}
System.out.println("Array after right rotation by "+n+" positions:");
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args)
{
rightRotate(new int[] {1,2,3,4,5,6,7}, 3);
}
}
Output of the above code:
Array before rotation:
[1, 2, 3, 4, 5, 6, 7]
Array after right rotation by 3 positions:
[5, 6, 7, 1, 2, 3, 4]
Related Articles
Sort array in ascending order Java
Automorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java