-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral2DArray.java
More file actions
43 lines (33 loc) · 839 Bytes
/
Spiral2DArray.java
File metadata and controls
43 lines (33 loc) · 839 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
public class Spiral2DArray {
public static void Spiral(int[][]arr){
int rows=arr.length;
int columns=arr[0].length;
int rowStart=0;
int rowEnd=rows-1;
int columnStart=0;
int columnEnd=columns-1;
while(columnStart>=columnEnd&&rowStart>=rowEnd)
{
for(int i=columnStart;i<=columnEnd;i++){
System.out.print(arr[rowStart][i]+" ");
}
columnStart=columnEnd;
columnEnd--;
for(int j=rowStart;j<=rowEnd;j++){
System.out.print(arr[j][columnStart]);
}
rowStart=rowEnd;
rowEnd--;
for(int k=columnStart;k>=columns-columnEnd;k--){
System.out.print(arr[rowStart][k]);
}
columnStart=columnEnd;
columnEnd--;
for(int m=rowStart;m>=rows-rowEnd;m--){
System.out.print(arr[m][columnStart]);
}
rowStart=rowEnd;
rowEnd--;
}
}
}