Rotation of Elements of Array - Left and Right in Java

Understanding Array Rotation

Array rotation involves shifting elements either to the left or right by a specified number of positions.

We will explore three different methods to perform left and right rotation in Java.

Method 1: Using Temporary Array

public class ArrayRotation {
    static void leftRotate(int arr[], int d) {
        int n = arr.length;
        int temp[] = new int[d];
        for (int i = 0; i < d; i++)
            temp[i] = arr[i];
        for (int i = 0; i < n - d; i++)
            arr[i] = arr[i + d];
        for (int i = 0; i < d; i++)
            arr[n - d + i] = temp[i];
    }

    static void rightRotate(int arr[], int d) {
        int n = arr.length;
        int temp[] = new int[d];
        for (int i = 0; i < d; i++)
            temp[i] = arr[n - d + i];
        for (int i = n - 1; i >= d; i--)
            arr[i] = arr[i - d];
        for (int i = 0; i < d; i++)
            arr[i] = temp[i];
    }

    static void printArray(int arr[]) {
        for (int num : arr)
            System.out.print(num + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        leftRotate(arr, 2);
        System.out.println("Array after left rotation:");
        printArray(arr);
        rightRotate(arr, 2);
        System.out.println("Array after right rotation:");
        printArray(arr);
    }
}            

Method 2: Using Reversal Algorithm

public class ArrayRotationReversal {
    static void reverse(int arr[], int start, int end) {
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }

    static void leftRotate(int arr[], int d) {
        int n = arr.length;
        reverse(arr, 0, d - 1);
        reverse(arr, d, n - 1);
        reverse(arr, 0, n - 1);
    }

    static void rightRotate(int arr[], int d) {
        int n = arr.length;
        reverse(arr, 0, n - 1);
        reverse(arr, 0, d - 1);
        reverse(arr, d, n - 1);
    }

    static void printArray(int arr[]) {
        for (int num : arr)
            System.out.print(num + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        leftRotate(arr, 2);
        System.out.println("Array after left rotation:");
        printArray(arr);
        rightRotate(arr, 2);
        System.out.println("Array after right rotation:");
        printArray(arr);
    }
}            

Method 3: Rotating One by One

public class ArrayRotationOneByOne {
    static void leftRotateOne(int arr[]) {
        int n = arr.length;
        int temp = arr[0];
        for (int i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[n - 1] = temp;
    }

    static void rightRotateOne(int arr[]) {
        int n = arr.length;
        int temp = arr[n - 1];
        for (int i = n - 1; i > 0; i--)
            arr[i] = arr[i - 1];
        arr[0] = temp;
    }

    static void leftRotate(int arr[], int d) {
        for (int i = 0; i < d; i++)
            leftRotateOne(arr);
    }

    static void rightRotate(int arr[], int d) {
        for (int i = 0; i < d; i++)
            rightRotateOne(arr);
    }

    static void printArray(int arr[]) {
        for (int num : arr)
            System.out.print(num + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        leftRotate(arr, 2);
        System.out.println("Array after left rotation:");
        printArray(arr);
        rightRotate(arr, 2);
        System.out.println("Array after right rotation:");
        printArray(arr);
    }
}            
Top 100 Codes By Learn-for-free
Start Preparing Arraysform here👇

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Find Largest Element in an Array: C C++ Java Python

Find Smallest Element in an Array: C C++ Java Python

Find the Smallest and Largest Element in an Array: C C++ Java Python

Find Second Smallest Element in an Array: C C++ Java Python

Calculate the Sum of Elements in an Array: C C++ Java Python

Reverse an Array: C C++ Java Python

Sort First Half in Ascending Order and Second Half in Descending: C C++ Java Python

Finding the Frequency of Elements in an Array: C C++ Java Python

Counting the Number of Even and Odd Elements in an Array: C C++ Java Python

Finding Maximum Product Sub-array in a Given Array: C C++ Java Python

Finding Arrays are Disjoint or Not: C C++ Java Python

Finding Equilibrium Index of an Array: C C++ Java Python

Rotation of Elements of Array - Left and Right: C C++ Java Python

Balanced Parenthesis Problem: C C++ Java Python