public class SpiralMatrix {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = new int[][]{ {1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
printSpiralMatrix(a,4,4);
String s = "sekhar";
s.substring(s.length());
s.substring(3, 3);
try{
String myString = "Indian";
//myString.substring(2,10);
}catch(Exception e){
e.printStackTrace();
}
}
private static void printSpiralMatrix(int[][] a, int m, int n) {
// m = total number of rows = 4
// n = total number of columns
int k = 0; // Row starts
int l = 0; // column starts
while(k<m && l<n){
//print first row....
for(int i = l;i<n;i++){
System.out.println(a[k][i]);
}
k++;
for(int i=k;i<m;i++){
System.out.println(a[i][n-1]);
}
n--;
for(int i= n-1; i>=l;--i){
System.out.println(a[m-1][i]);
}
m--;
for(int i=m-1;i>=k;--i){
System.out.println(a[i][l]);
}
l++;
}
}
}
Hi, This blog is the place who want to prepare for programming interview with major product development companies. Apart from interview questions I would like to provide some concepts on the new technologies which I learn as part of my carrier. Thanks for visiting my Blog.
Monday, October 17, 2011
printing spiral matrix in java
Subscribe to:
Posts (Atom)
AWS certification question
AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...
-
Here I am going to discuss some of the differences between Text and String class in Hadoop. Text class lies in the package: import org.ap...
-
Q: What is the library you used in spring to connect to database? Spring JDBC template. Q: What and all involved in fetching the data ...
-
There are 2 ways we can load data into the HIVE Tables. In this tutorial we are loading data into the HIVE managed Table. This is my sa...