Java Program to Rotate an Array

Array Rotation

Java program for array rotation has been shown here. An array rotation is described as the rotation of array elements to either left or right by k positions.


Example 1: Left rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the left.

=>A[] = {20, 30, 40, 50, 60, 10}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {30, 40, 50, 60, 10, 20}

So, array A[] after left rotation by 2 positions: {30, 40, 50, 60, 10, 20}



Example 2: Right rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the right.

=>A[] = {60, 10, 20, 30, 40, 50}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {50, 60, 10, 20, 30, 40}

So, array A[] after right rotation by 2 positions: {50, 60, 10, 20, 30, 40}






1. Java program & output to Rotate an Array

Code has been copied
/********************************
       alphabetacoder.com
Java program to rotate an array
*********************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int i, j, k, s, d, e;

        // take input
        System.out.print("Enter the size of array: ");
        s = sc.nextInt();

        // declare an array of size s
        int[] arr = new int[s];

        // take input
        System.out.print("Enter the elements of array: ");
        for (i = 0; i < s; i++) {
            arr[i] = sc.nextInt();
        }

        // take input of the direction of rotation
        System.out.println("Enter the direction of rotation: ");
        System.out.println("1. Left rotation");
        System.out.println("2. Right rotation");
        System.out.print("Your choice >> ");
        d = sc.nextInt();

        switch (d) {
            case 1:
                // take input of the number of position of rotation
                System.out.print("Enter the number of position to be left shifted: ");
                k = sc.nextInt();
    
                // left shift the array upto k position
                for (i = 0; i < k; i++) {
                    // get the first elements
                    e = arr[0];
                    for (j = 0; j < s - 1; j++) {
                        arr[j] = arr[j + 1];
                    }
                    arr[s - 1] = e;
                }
                // display the array
                System.out.print("Output array: ");
                for (i = 0; i < s; i++) {
                    System.out.print(arr[i] + " ");
                }
                break;
            case 2:
                // take input of the number of position of rotation
                System.out.print("Enter the number of position to be right shifted: ");
                k = sc.nextInt();
    
                // right shift the array upto k position
                for (i = 0; i < k; i++) {
                    // get the first elements
                    e = arr[s - 1];
                    for (j = s - 1; j > 0; j--) {
                        arr[j] = arr[j - 1];
                    }
                    arr[0] = e;
                }
                // display the array
                System.out.print("Output array: ");
                for (i = 0; i < s; i++) {
                    System.out.print(arr[i] + " ");
                }
                break;
            default:
                System.out.print("Wrong choice!");
                break;
        }
    }
}

Output


Case 1:

Enter the size of array: 5

Enter the elements of array: 0 5 10 15 20

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 1

Enter the number of position to be right shifted: 3

Output array:

15 20 0 5 10


Case 2:

Enter the size of array: 5

Enter the elements of array: 0 5 10 15 20

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 2

Enter the number of position to be left shifted: 3

Output array:

10 15 20 0 5



2. Time complexity to Rotate an Array


Time Complexity: O(n * k)

Here, $n$ elements in an array are to be rotated by $k$ positions to either left or right.