Array Rotation in Java

(Asked by top MNC’s like Amazon, Microsoft, goldman sachs, wipro, TCS..etc)

Aarray rotation means to rotate the elements of an array by given positions.

In the above image we have given an array of size 5 and we have to rotate it left by k positions.

So to rotate array by K position we will use the method name called rotate one by one.

To rotate it one by one we take one temp variable and intilize this variable by arr[0].

After that we will move the position of array elements by 1 towards left so arr[1] will come at position arr[0] and so on.

And at last we will place the temp value at arr[n-1] = temp.

Simple algorithm:

leftRotate(arr[], k, n)
start
  For i = 0 to i < k // the rorate one by one process will be executed here k time 
    Left rotate all elements of arr[] by one
end

Let’s see the code implementation:

package arrays;

 class RotateArray { 
    /*Function to left rotate arr[] of size n by k*/
    void leftRotate(int arr[], int k, int n) 
    { 
        for (int i = 0; i < k; i++) 
            leftRotatebyOne(arr, n); /* the leftRotateByOne method will runs k time here  */
    } 
  
    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; 
    } 
  
    
  
    // main method 
    public static void main(String[] args) 
    { 
        RotateArray rotate = new RotateArray(); 
        int arr[] = { 1, 2, 3, 4, 5, 6 }; 
        rotate.leftRotate(arr, 2, 6); 
        //print array using for each loop
           for(int ans:arr)
        {
            System.out.print(ans + " ");
         }
    } 
}

Output:

3 4 5 6 1 2

We will solve the above code using StringBuilder class also, so let’s see the implementations of this also:

StringBuilder class creates the object of mutable string in java.

package array;

import java.util.Scanner;

public class RotateArray {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t = sc.nextInt();
		 while(t-->0){
		     int n = sc.nextInt();
		     int [] arr = new int[n];
		     for(int i =0; i<n; i++){
		         arr[i] = sc.nextInt();
		     }
		          int d = sc.nextInt();
		          StringBuilder sb = new StringBuilder();
		          
		          if(d <= n) {
		        	  for(int i=d; i<n; i++) {
			        	  sb.append(arr[i]+" ");
			          }
			          for(int i=0; i<d; i++) {
			        	  sb.append(arr[i] + " ");
			          }
			          
			          System.out.println(sb );
		        	  
		          } else {
		        	  System.out.println("rotate value is grater than arry pleas enter small value");
		          }
		          
 }
		}
	}


So i hope you all guys understand the implementation of Array Rotation by using both methods.

For any kind queries regarding the blog please comment below and keep learning.

Thanks for reading…!

Happy Coding…!

Thank You..!



Categories: HackerRank Questions, Interview, JAVA, Uncategorized

Tags: , , , , , , , , , , , , , , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: