Java program to right rotate array

In this tutorial, I will explain how to right rotate the elements of an array with a simple Java program. Let us get started.

Right Rotate the elements of an array in Java

Right rotating the elements of an array ‘k’ times means to shift all the elements ‘k’ places to their right. The last element will acquire the first position after each shift. I will explain this with an example:

Original Array : [10, 15, 20, 25, 0]

Right Rotate the array by 1 time.

Resultant Array : [0, 10, 15, 20, 25]

As you can see in the resultant array, all the elements are shifted one position to their right. The last element in the original array acquires the first position now.

I will be using OOPs concepts of Java. I have made two functions in my class: rotate() and main() function.

The rotate() function takes three parameters passed as arguments. First, I have stored the last element in a temporary variable. Next, inside a for loop, I assign each index with the value of its preceding index. At last, outside the loop, I assign the value in the temporary variable to the first index of our array. I have nested this whole process inside another for loop. I have done this to give the added functionality of right rotating the array ‘k’ times. Once I have obtained the resultant array, I print it using the for loop again.

In the main() function, I have made an object of the Scanner class for user input. I take the various inputs and make the object of our class. With this instance of our class, I call the rotate() function with the necessary parameters passed as arguments.

I have shown my code below:

import java.util.*;
public class RightRotateArray 
{
    void rotate(int a[], int n, int r)
    {
        int i;
        System.out.println("Original Array:     ");
        
        for(i=0;i<n;i++)
        {
            System.out.print(a[i] + " ");
        }

        //j loop to right rotate k times
        for (int j=1;j<=r;j++)
        {
            //store last element in a temporary variable
            int x = a[n-1];
            for(i=n-1;i>0;i--)
            {
                //shift all elements
                a[i] = a[i-1];
            }
            //assign new value to first index
            //shifting of last element to the first position
            a[0] = x;

        }

        System.out.println(" ");
        System.out.println("Right Rotated Array: ");

        for(i=0;i<n;i++)
        {
            System.out.print(a[i] + " ");
        }
    }
    
    public static void main(String[] args)
    {
        Scanner ob = new Scanner(System.in);
        System.out.println("Enter the number of elements in the array");
        int n = ob.nextInt();
        int A[] = new int[n];
        System.out.println("Enter the number of times the array needs to be rotated right");
        int r = ob.nextInt();
        System.out.println("Enter elements in array");
        for(int j=0;j<n;j++)
        {
            A[j] = ob.nextInt();
        }

        RightRotateArray rra = new RightRotateArray();

        rra.rotate(A, n, r);
    }
}

Input:

I take user input for the number of elements in the array, the number of times the array needs to be right rotated, and the elements of the array.

Output:

Enter the number of elements in the array
5
Enter the number of times the array needs to be rotated right
1
Enter elements in array

Original Array:
10 15 20 25 0
Right Rotated Array:
0 10 15 20 25

Also read: Left rotate an array by D places in Java

Leave a Reply

Your email address will not be published. Required fields are marked *