Skip to content

Instantly share code, notes, and snippets.

@Ram-1234
Created June 19, 2021 02:17
Show Gist options
  • Save Ram-1234/c20bdd72ad951cf808dce638b198f2fd to your computer and use it in GitHub Desktop.
Save Ram-1234/c20bdd72ad951cf808dce638b198f2fd to your computer and use it in GitHub Desktop.
Array Rotation in Java
Description:
Before Rotation
1 2 3
4 5 6
7 8 9
after rotation
7 4 1
8 5 2
9 6 3
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
int [][] arr=new int[s][s];
for(int i=0;i<s;i++){
for(int j=0;j<s;j++){
arr[i][j]=sc.nextInt();
}
}
System.out.println("Before Rotation");
for(int i=0;i<s;i++){
for(int j=0;j<s;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
System.out.println("after rotation");
for(int i=0;i<s;i++){
for(int j=s-1;j>=0;j--){
System.out.print(arr[j][i]+" ");
}
System.out.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment