GIven a 2D NxN matrix, visualize it as concentric circles. You have to find the rotated matrix where each element in the circle is rotated by 1 position layer by layer in an alternate clockwise and anticlockwise direction.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.lang.Math;

public class Solution {

public static void main(String[] args) {

int N =0;

int a[][],b[][];

BufferedReader br =

new BufferedReader(new InputStreamReader(System.in));

try {

String s = br.readLine();

N = Integer.parseInt(s);

a = new int[N][N];

for(int i=0;i<N;i++)

{

s = br.readLine();

String tmp[] =s.split(" ");

for(int j=0;j<N;j++){

a[i][j]= Integer.parseInt(tmp[j]);

//System.out.print(a[i][j]+" ");

}

//System.out.println();

}

int dist = 0;

int ii,jj;

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

dist = Math.min(Math.min(i,N-1-i),Math.min(j,N-1-j));

ii=i;jj=j;

if(dist%2==0){ // do clockwise rotation

if(dist==i){// top row

jj = j-1;

ii = i;

if(jj<dist){

jj = j;

ii = i+1;

}

}

else if(dist==j){// left col

jj = j;

ii = i+1;

if((N-1-ii)<dist){

jj = j+1;

ii = i;

}

}

else if(dist==(N-1-i)){// bottom row

jj = j+1;

ii = i;

if((N-1-jj)<dist){

jj = j;

ii = i-1;

}

}

else if(dist==(N-1-j)){// right col

jj = j;

ii = i-1;

if(ii<dist){

jj = j-1;

ii = i;

}

}

}

else{ // anti clockwise

if(dist==i){// top row

jj = j+1;

ii = i;

if((N-1-jj)<dist){

jj = j;

ii = i+1;

}

}

else if(dist==j){// left col

jj = j;

ii = i-1;

if(ii<dist){

jj = j+1;

ii = i;

}

}

else if(dist==(N-1-i)){// bottom row

jj = j-1;

ii = i;

if(jj<dist){

jj = j;

ii = i-1;

}

}

else if(dist==(N-1-j)){// right col

jj = j;

ii = i+1;

if((N-1-ii)<dist){

jj = j-1;

ii = i;

}

}

}

if(Math.min(ii,N-1-ii)<dist){

ii=i;

}

else if(Math.min(jj,N-1-jj)<dist){

jj=j;

}

System.out.print(a[ii][jj]+" ");

}

System.out.println();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

**Sample Input:**

4

2 3 4 5

1 6 7 8

4 2 1 9

5 3 2 4

**Sample Output:**

1 2 3 4

4 7 1 5

5 6 2 8

3 2 4 9

**My Code:**import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.lang.Math;

public class Solution {

public static void main(String[] args) {

int N =0;

int a[][],b[][];

BufferedReader br =

new BufferedReader(new InputStreamReader(System.in));

try {

String s = br.readLine();

N = Integer.parseInt(s);

a = new int[N][N];

for(int i=0;i<N;i++)

{

s = br.readLine();

String tmp[] =s.split(" ");

for(int j=0;j<N;j++){

a[i][j]= Integer.parseInt(tmp[j]);

//System.out.print(a[i][j]+" ");

}

//System.out.println();

}

int dist = 0;

int ii,jj;

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

dist = Math.min(Math.min(i,N-1-i),Math.min(j,N-1-j));

ii=i;jj=j;

if(dist%2==0){ // do clockwise rotation

if(dist==i){// top row

jj = j-1;

ii = i;

if(jj<dist){

jj = j;

ii = i+1;

}

}

else if(dist==j){// left col

jj = j;

ii = i+1;

if((N-1-ii)<dist){

jj = j+1;

ii = i;

}

}

else if(dist==(N-1-i)){// bottom row

jj = j+1;

ii = i;

if((N-1-jj)<dist){

jj = j;

ii = i-1;

}

}

else if(dist==(N-1-j)){// right col

jj = j;

ii = i-1;

if(ii<dist){

jj = j-1;

ii = i;

}

}

}

else{ // anti clockwise

if(dist==i){// top row

jj = j+1;

ii = i;

if((N-1-jj)<dist){

jj = j;

ii = i+1;

}

}

else if(dist==j){// left col

jj = j;

ii = i-1;

if(ii<dist){

jj = j+1;

ii = i;

}

}

else if(dist==(N-1-i)){// bottom row

jj = j-1;

ii = i;

if(jj<dist){

jj = j;

ii = i-1;

}

}

else if(dist==(N-1-j)){// right col

jj = j;

ii = i+1;

if((N-1-ii)<dist){

jj = j-1;

ii = i;

}

}

}

if(Math.min(ii,N-1-ii)<dist){

ii=i;

}

else if(Math.min(jj,N-1-jj)<dist){

jj=j;

}

System.out.print(a[ii][jj]+" ");

}

System.out.println();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

## No comments:

## Post a Comment