-
Notifications
You must be signed in to change notification settings - Fork 370
/
SPIRALDISPLAY.java
109 lines (74 loc) · 1.64 KB
/
SPIRALDISPLAY.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.Scanner;
public class SPIRALDISPLAY {
public static void Display(int [][] a)
{
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int row = scn.nextInt();
int coloumn = scn.nextInt();
int [][] spiralArray = new int[row][coloumn];
for (int i = 0; i < spiralArray.length; i++) {
for (int j = 0; j < spiralArray[0].length; j++) {
spiralArray[i][j] = scn.nextInt();
}
}
int minr = 0;
int minc = 0;
int maxr = spiralArray.length-1;
int maxc = spiralArray[0].length-1;
int tne = row * coloumn;
int cnt = 0;
// Display(spiralArray);
while(cnt < tne)
{
// LEFT WALL
for(int i = minr , j=minc ; i <= maxr && cnt < tne; i++ )
{
System.out.println(spiralArray[i][j]);
cnt++;
}
minc++;
// BOTTOM WALL
for(int i = maxr , j=minc ; j <= maxc && cnt < tne; j++ )
{
System.out.println(spiralArray[i][j]);
cnt++;
}
maxr--;
for(int i = maxr , j=maxc ; i >= minr && cnt < tne; i-- )
{
System.out.println(spiralArray[i][j]);
cnt++;
}
maxc--;
for(int i = minr , j=maxc ; j >= minc && cnt < tne; j-- )
{
System.out.println(spiralArray[i][j]);
cnt++;
}
minr++;
}
}
}
//11
//21
//31
//32
//33
//34
//35
//25
//15
//14
//13
//12
//22
//23
//24