"Coding is just like talking to a unknown girl. Once you talked to her with guts, the next time you'll never afraid",Really? Yes , try it Now !!!
Everything here is for education and learning purpose only , individual holds rights on outer posts/links etc.
Team Cracking The Code
About Me
▼
Sunday, April 1, 2012
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.
/**
ReplyDelete*
* @author Vikky
*/
import java.util.*;
public class CircularLoop {
int dimension=0;
int arr[][];
public static void main(String args[])
{
CircularLoop obj=new CircularLoop();
obj.arr=new int[5][5];
int temp=1;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
obj.arr[i][j]=temp++;
}
}
//obj.print(obj.arr);
for(int i=0;i<5;i++)
{
obj.store(i, 5);
}
}
void store(int i,int len)
{
ArrayList ar=new ArrayList();
int temp=0;
for(int k=i;k=i;k--)
{
ar.add(arr[len-i-1][k]);
}
for(int k=len-i-2;k>i;k--)
{
ar.add(arr[k][i]);
}
this.print1(ar);
if(i%2==0)
this.cw(ar);
else
this.acw(ar);
}
void print(int [][]arr)
{
System.out.println("");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.println(arr[i][j]);
}
}
}
void print1(ArrayList ar)
{
System.out.println("");
Iterator ir=ar.iterator();
while(ir.hasNext())
{
System.out.print(ir.next()+" ");
}
}
void acw(ArrayList ar)
{
if(ar.size()>0)
{
int size=ar.size();
ArrayList aa=new ArrayList();
aa.add(ar.get(size-1));
for(int i=0;i ar)
{
if(ar.size()>0)
{
int size=ar.size();
ArrayList aa=new ArrayList();
aa.add(0, ar.get(size-1));
for(int i=0;i<size-1;i++)
aa.add(ar.get(i));
System.out.println("\nClockwise :");
this.print1(aa);
}
}
}
@Vikky
ReplyDeletecan you give more insight by giving algo ?