Skip to content
Related Articles

Related Articles

How to Left or Right rotate an Array in Java?

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 23 Jan, 2022
Improve Article
Save Article

Given an array arr[] of size N and D index, the task is to rotate the array by the D index. We have two flexibilities either to rotate them leftwards or rightwards via different ways which we are going to explore by implementing every way of rotating in both of the rotations.

Ways:

  1. Using temporary array
  2. Recursively rotating array one by one
  3. Using Juggling Algorithm

Left Rotation of Array

Illustration:

Input  : arr[] = {1, 2, 3, 4, 5} 
D = 2 
Output : 3 4 5 1 2 

Output explanation: 

  • The initial 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 

Way 1: Using a temporary array

Approach:

In this method simply create a temporary array and copy the elements of the array arr[] from 0 to the (D-1)th 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 
  • Store the first d elements in a temp array: temp[] = [1, 2]
  • Shift rest of the arr[]: arr[] = [3, 4, 5]
  • Store back the D elements: arr[] = [3, 4, 5, 1, 2]

Example:

Java




// Java program to Left Rotate an Array
// by D elements
 
// Main class
class GFG {
 
    // Method 1
    // To left rotate arr[]
    // of size N by D
    void leftRotate(int arr[], int d, int n)
    {
        // Creating temp array of size d
        int temp[] = new int[d];
 
        // Copying first d element in array temp
        for (int i = 0; i < d; i++)
            temp[i] = arr[i];
 
        // Moving the rest element to index
        // zero to N-d
        for (int i = d; i < n; i++) {
            arr[i - d] = arr[i];
        }
 
        // Copying the temp array element
        // in original array
        for (int i = 0; i < d; i++) {
            arr[i + n - d] = temp[i];
        }
    }
 
    // Method 2
    // To print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of class inside main()
        GFG rotate = new GFG();
 
        // Custom input array
        int arr[] = { 1, 2, 3, 4, 5 };
 
        // Calling method 1 and 2 as defined above
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}

Output

3 4 5 1 2 

Time complexity: O(N) , Auxiliary Space: O(D)

Way 2: Rotate one by one

Approach:

Rotate the array recursively one by one element

Input arr[] = [1, 2, 3, 4, 5]
D = 21
  • Swap arr[0] to arr[1]
  • Swap arr[1] to arr[2]
  • Swap arr[N-1] to arr[N]
  • Repeat 1, 2, 3 to D times

In order to 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]

Illustration:

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.

Example

Java




// Java program to left rotate an array
// by Rotate one by one
 
// Main class
class GFG {
 
    // Method 1
    // To rotate left by D elements
    void leftRotate(int arr[], int d, int n)
    {
        for (int i = 0; i < d; i++)
            leftRotatebyOne(arr, n);
    }
 
    // Method 2
    // To rotate left one by one
    void leftRotatebyOne(int arr[], int n)
    {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[i] = temp;
    }
 
    // Method 3
    // To print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG rotate = new GFG();
 
        // Custom input array
        int arr[] = { 1, 2, 3, 4, 5 };
 
        // Calling method to rotate array leftwards
        // and later printing the array elements
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}

Output

3 4 5 1 2 

Time complexity: O(N * D), Auxiliary Space: O(1) 

Way 3: Using 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.

Example:

Java




// Java program to left rotate an array by d elements
// using Juggling Algorithm
 
// Main class
class GFG {
 
    // Method 1
    // To left rotate arr[] of siz N by D
    void leftRotate(int arr[], int d, int n)
    {
        // To handle if d >= n
        d = d % n;
        int i, j, k, temp;
        int g_c_d = gcd(d, n);
 
        for (i = 0; i < g_c_d; i++) {
 
            // move i-th values of blocks
            temp = arr[i];
            j = i;
 
            // Performing sets of operations if
            // condition holds true
            while (true) {
                k = j + d;
                if (k >= n)
                    k = k - n;
                if (k == i)
                    break;
                arr[j] = arr[k];
                j = k;
            }
            arr[j] = temp;
        }
    }
 
    // Method 2
    // To print an array
    void printArray(int arr[], int size)
    {
        int i;
        for (i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Method 3
    // To get gcd of a and b
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
 
    // Method 4
    // main driver method
    public static void main(String[] args)
    {
        // Creating instance of class inside main() method
        GFG rotate = new GFG();
 
        // Custom array elements
        int arr[] = { 1, 2, 3, 4, 5 };
 
        // Calling above methods inside main() to
        // left rotate and print the array elements
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}

Output

3 4 5 1 2 

Time complexity: O(n) , Auxiliary Space: O(1) 

Right Rotation of Array

Illustration:

Input  : arr[] = {1, 2, 3, 4, 5} 
         D = 2
Output : 4 5 1 2 3 

Output explanation:

  • The initial 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  

Way 1: 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. It is as illustrated below illustration as follows:

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]

Example:

Java




// Java program to rotate an array by D elements
 
// Main class
class GFG {
 
    // Method 1
    // To right rotate arr[] of size N by D
    void rightRotate(int arr[], int d, int n)
    {
 
        // If arr is rotated n times then
        // you get the same array
        while (d > n) {
            d = d - n;
        }
 
        // Creating a temporary array of size d
        int temp[] = new int[n - d];
 
        // Now copying first N-D element in array temp
        for (int i = 0; i < n - d; i++)
            temp[i] = arr[i];
 
        // Moving the rest element to index zero to D
        for (int i = n - d; i < n; i++) {
            arr[i - n + d] = arr[i];
        }
 
        // Copying the temp array element
        // in original array
        for (int i = 0; i < n - d; i++) {
            arr[i + d] = temp[i];
        }
    }
 
    // Method 2
    // To print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
 
            // Printing elements of an array
            System.out.print(arr[i] + " ");
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating object of class inside main() method
        GFG rotate = new GFG();
 
        // Custom input array
        int arr[] = { 1, 2, 3, 4, 5 };
 
        // Calling method1 to rotate array
        rotate.rightRotate(arr, 2, arr.length);
 
        // Calling method 2 to print array
        rotate.printArray(arr, arr.length);
    }
}

Output

4 5 1 2 3 

Time complexity: O(N) , Auxiliary Space: O(D)

Way 2: Rotate one by one 

Approach: Rotate the array recursively one by one element

Input arr[] = [1, 2, 3, 4, 5], D = 2
  • swap arr[N] to arr[N-1]
  • swap arr[N-1] to arr[N-2]
  • swap arr[2] to arr[1]
  • Repeat 1, 2, 3 to D times

To 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.

 Example:

Java




// Java Program to Rotating Elements One by One
 
// Main class
class GFG {
 
    // Method 1
    // To right rotate array of size n by d
    void rightRotate(int arr[], int d, int n)
    {
        // Iterating till we want
        for (int i = n; i > d; i--)
 
            // Recursively calling
            rightRotatebyOne(arr, n);
    }
 
    // Method 2
    // To rotate array by 1
    void rightRotatebyOne(int arr[], int n)
    {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[i] = temp;
    }
 
    // Method 3
    // To print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of class inside main()
        GFG rotate = new GFG();
 
        // Custom input elements
        int arr[] = { 1, 2, 3, 4, 5 };
 
        // Calling method 1 and 2
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}

Output

4 5 1 2 3 

Here we have rotated 2 elements one by one else if we would have had rotated by one element only then the array would have been [2,3,4,5,1]

Time complexity: O(N * D) , Auxiliary Space: O(1) 
 

Way 3: 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.
 

Java




// Java Program to Rotate Array Elements
// Using Juggling Algorithm
 
// Main class
class GFG {
 
    // Method 1
    // To right rotate arr[]
    // of siz N by D
    void rightRotate(int arr[], int d, int n)
    {
 
        // To use as left rotation
        d = n - d;
        d = d % n;
        int i, j, k, temp;
        int g_c_d = gcd(d, n);
 
        for (i = 0; i < g_c_d; i++) {
 
            // Moving i-th values of blocks
            temp = 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;
        }
    }
 
    // Method 2
    // To print an array
    void printArray(int arr[], int size)
    {
 
        int i;
        for (i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Method 3
    // To get gcd of a and b
    int gcd(int a, int b)
    {
 
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
 
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG rotate = new GFG();
 
        // Custom input elements
        int arr[] = { 1, 2, 3, 4, 5 };
 
        // Calling methods 1 and 2
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}

Output

4 5 1 2 3 

Time complexity: O(n) , Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!