Java Program to rotate the elements of an array to the right

In this program, our objective is to rotate the elements of an array towards the right by a specified number of times. To achieve this, we can adopt an approach that involves looping through the collection and shifting each element to its next position. In a proper rotation, the last element of the array becomes the first element of the rotated array. By iterating through the collection and shifting elements accordingly, we can successfully achieve the right rotation effect.

In the given array, when the specified number of rotations, denoted by n, is equal to 1, each array element will be shifted one position to the right. This means that the first element will move to the second position, the second element will be shifted to the third position, and so on. Consequently, the last element of the array will wrap around and become the new first element.

Algorithm for a Java program to rotate the elements of an array to the right

STEP 1: START

STEP 2: Initialize the array arr[] with values {1, 2, 3, 4, 5}

STEP 3: Set the variable n to 3

STEP 4: Print the message “Original Array”

STEP 5: Repeat steps 6 to 7 until i is less than the length of arr[] // for(i=0; i<arr.length; i++)

STEP 6: Print the value of arr[i]

STEP 7: Repeat steps 8 to 12 until i is less than n // for(i=0; i<n; i++)

STEP 8: Define the variables j and last

STEP 9: Set last to the value of arr[arr.length-1]

STEP 10: Repeat steps 11 to 12 until j is greater than 0 // for(j=arr.length-1; j>0; j–)

STEP 11: Set arr[j] to the value of arr[j-1]

STEP 12: Set arr[0] to the value of last

STEP 13: Print the message “Array after right rotation”

STEP 14: Repeat steps 15 to 16 until i is less than the length of arr[] // for(i=0; i<arr.length; i++)

STEP 15: Print the value of arr[i]

STEP 16: END

Java Program to rotate the elements of an array to the right

class RotateRight {    
 public static void main(String[] args) {    
        //Initialize array     
        int [] arr = new int [] {1, 2, 3, 4, 5};     
        //n determine the number of times an array should be rotated.    
        int n = 3;    
           
        //Displays original array    
        System.out.println("Original array: ");    
        for (int i = 0; i < arr.length; i++) {     
            System.out.print(arr[i] + " ");     
        }      
            
        //Rotate the given array by n times toward right    
        for(int i = 0; i < n; i++){    
            int j, last;    
            //Stores the last element of array    
            last = arr[arr.length-1];    
            
            for(j = arr.length-1; j > 0; j--){    
                //Shift element of array by one    
                arr[j] = arr[j-1];    
            }    
            //Last element of array will be added to the start of array.    
            arr[0] = last;    
        }    
        
        System.out.println();    
            
        //Displays resulting array after rotation    
        System.out.println("Array after right rotation: ");    
        for(int i = 0; i< arr.length; i++){    
            System.out.print(arr[i] + " ");    
        }    
    }    
}

Output:

Original Array:
1   2   3   4   5
Array after right rotation:
3   4   5   1   2

In conclusion, the provided Java program effectively rotates the elements of an array to the right by a specified number of positions, achieving a right rotation effect. Follow tutorials.freshersnow.com to learn more.