java program to rotate an array n time

 Rotating an array n number of time

  • public class ArrayRotation {

  • public static void main(String[] args) {
  • int rotate= 5;
  • int arr2[]={0,1,2,3,4,5,6,7,8,9,10};
  • int temp=0;
  • int a;
  • int b;
  • while(rotate!=0){
  • for(int i=0;i<arr2.length;i++){
  • if(i==0)
  • {
  • a=arr2[i];
  • arr2[i]=arr2[arr2.length-1];
  • }
  • else a=temp;
  • if(i<arr2.length-1)
  •  b=arr2[i+1];
  • else b=0;
  • temp=b;
  • if(i<arr2.length-1)
  • arr2[i+1]=a;

  • System.out.print(arr2[i]+" ");
  • }
  • rotate--;
  • System.out.println();
  • }
  • }
  • }
Output


10 0 1 2 3 4 5 6 7 8 9 
9 10 0 1 2 3 4 5 6 7 8 
8 9 10 0 1 2 3 4 5 6 7 
7 8 9 10 0 1 2 3 4 5 6 
6 7 8 9 10 0 1 2 3 4 5 

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation