Skip to content

Instantly share code, notes, and snippets.

@mohitkhosla01
Created January 27, 2017 12:53
Show Gist options
  • Save mohitkhosla01/485b1c2ef2ed188ab99426e8e9837239 to your computer and use it in GitHub Desktop.
Save mohitkhosla01/485b1c2ef2ed188ab99426e8e9837239 to your computer and use it in GitHub Desktop.
matrixLayerRotation2
package hackerrank_algorithm;
import java.util.Scanner;
public class matrixLayerRotation2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int r=sc.nextInt();
int [][]arr=new int[m][n];
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
arr[i][j]=sc.nextInt();
}
}
int w=0;
int [][]brr=new int[m][n];
O: for(int i=0;i<m;++i){
I: for(int j=0;j<n;++j){
System.out.println("i: "+i+"\tj: "+j);
int p=((i+1)>(m/2))?(m-i-1):i;
int q=((j+1)>(n/2))?(n-j-1):j;
int min=Math.min(p,q);
int x=m-(2*min);
int y=n-(2*min);
w=(2*x)+(2*y)-4;
r%=w;
// System.out.print(x+","+y+"\t");
if(i<(m/2) && i!=j){
int L=y-j;
if(L>=r){
brr[i][j-r]=arr[i][j];
continue I;
}
r-=L;
int B=x-1;
if(B>=r){
brr[i+r][i]=arr[i][j];
continue I;
}
r-=B;
int R=y-1;
if(B>=r){
brr[x-1][r]=arr[i][j];
continue I;
}
r-=R;
int T=x-1;
if(T>=r){
brr[x-r-1][y-1]=arr[i][j];
continue I;
}
r-=T;
brr[i][y-r-1]=arr[i][j];
}
}
System.out.println();
}
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
System.out.print(brr[i][j]+"\t");
}
System.out.println();
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment