Array Rotation

Rotation of an array at the given Index

This page has the solution to the program in which we have to rotate the elements of an array over their indices. For example an array is entered Arr={1,2,3,4,5,6,7,8,9} and it is asked to rotate this array to its left by 2 positions then the Output will be Arr={3,4,5,6,7,8,9,1,2} or to its right by 2 positions then the output will be Arr={8,9,1,2,3,4,5,6,7}.

introduction to arrays in V

Steps for Array Rotation in JAVA

There are following Step of Array Rotation

  • Take a Temporary variable in which we’ll store the first element of the array if the rotation is in left direction or the last element in case of right rotation.
  • One by one store the elements in their left/right adjacent indices accordingly
  • Now store the element in the temporary variable back to the last or first index.
Rotation of an Array

Algorithm for Array Rotation

  • Rotate(int [] arr, int n)
  • FOR i from 1 to pivot index:
  • temp= last element/initial element
  • FOR i=0 to i=arr.length
  • arr[j]=arr[j-1]/arr[j+1]
  • END of FOR
  • arr[0] / arr[arr.length} = temporary
  • END of FOR

Program for Array Rotation in JAVA

Java Code

Run
import java.util.*;
class Main 
{
    void rightRotate(int[] arr, int n)
    {
        int temp;
        for (int i = 1; i <= n; i++) 
            { 
            temp = arr[arr.length-1]; 
            for (int j = arr.length-1; j > 0; j--) 
            {
                arr[j] = arr[j-1];
            }
            arr[0] = temp;
        }
        System.out.println("Input Array After Right Rotation By "+n+" Positions :");
        System.out.println(Arrays.toString(arr));
    }
    
    void leftRotate(int[] arr, int n) 
    {
        int temp;
        for (int i = 0; i < n; i++)
        {
            temp = arr[0];
            for (int j = 0; j < arr.length-1; j++) 
            {
                arr[j] = arr[j+1];
            }
            arr[arr.length - 1] = temp;
        }
        System.out.println("Input Array After Left Rotation By "+n+" Positions :");
        System.out.println(Arrays.toString(arr));
    }
     
    public static void main(String[] args) 
    {
        Scanner s= new Scanner(System.in);
        System.out.println("Enter the length of the array you wish to rotate: ");
        int l=s.nextInt();
        int arr1[]=new int[l];
        int arr2[]=new int[l];
        System.out.println("Enter the Elements");
        for(int i=0 ; i < l; i++)
        {
            arr1[i]=s.nextInt();
            arr2[i]=arr1[i];
        }
        System.out.println("Enter the index of rotation");
        int r=s.nextInt();
        System.out.println("Input Array Before Rotation :");
        System.out.println(Arrays.toString(arr1));
        Main m= new Main();
        m.rightRotate(arr1, r);
        m.leftRotate(arr2, r);
        
    }
}

Output:

Enter the length of the array you wish to rotate: 
7
Enter the Elements
1
2
3
4
5
6
7
Enter the index of rotation
3
Input Array Before Rotation :
[1, 2, 3, 4, 5, 6, 7]
Input Array After Right Rotation By 3 Positions :
[5, 6, 7, 1, 2, 3, 4]
Input Array After Left Rotation By 3 Positions :
[4, 5, 6, 7, 1, 2, 3]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Arrays

  • Introduction to Arrays – CC++ | Java
  • Introduction to 2-D Arrays – CC++ Java
  • Sorting of Array – C | C++ | Java
  • Array Rotation – CC++ | Java
  • Reverse an array or string- CC++ | Java
  • Find pairs in array with given sum – CC++ | Java
  • Sort the array in Waveform – CC++ | Java
  • Majority Element in Array – CC++ | Java
  • Boyer-Moore’s Voting Algorithm – CC++ | Java
  • K-pairs with smallest sum in 2 arrays – C | C++ | Java
  • Largest Sum Contigous SubArray – CC++ | Java
  • Maximum Average Sub-array of K length – CC++ | Java
  • Size of sub-array with max sum – CC++ | Java
  • Sub-array with given sum – CC++ | Java
  • Triplet that sum to a given value – CC++ | Java
  • Segregate 0’s and 1’s in array – CC++ | Java
  • Segregate 0’s 1’s and 2’s in array – CC++ | Java
  • Sort elements in array by frequency – CC++ | Java
  • Finding pythagorean triplets in an array – CC++ | Java
  • Reorder array using given indexes – CC++ | Java
  • Merging two sorted arrays – CC++ | Java
  • Minimum number of Merge Operations to make an Array Palindrome – CC++ | Java
  • Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – CC++ | Java

One comment on “Array Rotation”


  • ajit

    package com.company1;
    import java.util.Arrays;
    import java.util.Scanner;

    public class Main
    {

    public static int[] rotation(int[] arr,int k)
    {
    int j = 0;

    while(k>0)
    {
    int temp = arr[arr.length-1];

    for(j=arr.length-1;j>0;j–)
    {

    arr[j] = arr[j-1];

    }

    arr[j] = temp;
    k–;

    }

    return arr;
    }

    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println(“how many numbers you want to put in arrays ?”);
    int number = sc.nextInt();
    System.out.println(“now print those ” + number + ” arrays :”);

    int[] originalArray = new int[number];

    for(int i=0;i<originalArray.length;i++)
    {
    int values = sc.nextInt();
    originalArray[i] = values;

    }

    int[] duplicateArray = new int[originalArray.length];
    duplicateArray = Arrays.copyOf(originalArray,originalArray.length);

    System.out.println("now everything is set ,just tell me to how many places you have to right rotate your arrays?");
    int k = sc.nextInt();

    int[] printArray = rotation(duplicateArray,k);

    System.out.println("Here is your original array :");
    for(int i=0;i<originalArray.length;i++)
    {
    System.out.print(" " + originalArray[i]);
    }

    System.out.println();
    System.out.println("Here is your operated array");
    System.out.println();
    for(int i=0;i<printArray.length;i++)
    {
    System.out.print(" " + printArray[i]);
    }

    }

    }