-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprialMatrix.java
More file actions
59 lines (48 loc) · 979 Bytes
/
SprialMatrix.java
File metadata and controls
59 lines (48 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class SprialMatrix {
public static void printSpiralMatrix(int[][] arr,int m ,int n)
{
int l = 0;
int k = 0;
int a = n-1;
int b = m -1;
int c = 0;
while(k < m && l < n)
{
//printing the 1st row of the matrix //m denotes row and n denotes columns
for(int j = l; j < n ; j++ )
{
System.out.print(arr[k][j]+" ");
}
k++;
//printing the last column of the matrix
for(int i = k; i < m ; i++)
{
System.out.print(arr[i][a]+" ");
}
a--;
//printing the last row of the matrix
for(int j = a; j >= l ; j--)
{
System.out.print(arr[b][j]+ " ");
}
b--;
//printing the 1st column of the matrix
for(int i = b; i > l; i--)
{
System.out.print(arr[i][c]+" ");
}
c++;
l++;
n--;
m--;
}
}
public static void main(String[] args)
{
//int a[][] = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
int a[][] = new int[][]{{1,2,3,4}, {5,6,7,8}};
int m = 2;
int n = 4;
printSpiralMatrix(a, m , n);
}
}