-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_matrix.java
More file actions
62 lines (51 loc) · 1.24 KB
/
spiral_matrix.java
File metadata and controls
62 lines (51 loc) · 1.24 KB
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
60
61
62
import java.util.*;
class spiral_matrix{
public static void mat(int a[][]){
int rs=0;
int re=a.length-1;
int cs=0;
int ce=a[0].length-1;
while(ce>=cs && re>=rs){
for(int i=cs;i<=ce;i++){
System.out.print(a[cs][i]+" ");
}
for(int i=rs+1;i<=re;i++){
System.out.print(a[i][ce]+ " ");
}
if(rs==re){
break;
}
else{
for(int i=ce-1;i>=cs;i--){
System.out.print(a[re][i]+" ");
}
}
if(cs==ce){
break;
}
else{
for(int i=re-1;i>=rs+1;i--){
System.out.print(a[i][cs]+" ");
}
}
rs++;
re--;
cs++;
ce--;
}
}
public static void main(String args[]){
Scanner sc =new Scanner(System.in);
System.out.print("enter number of row : ");
int n=sc.nextInt();
System.out.print("enter the number of column : ");
int m=sc.nextInt();
int a[][]= new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=sc.nextInt();
}
}
mat(a);
}
}