-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakeMatrix.java
44 lines (38 loc) · 1022 Bytes
/
SnakeMatrix.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
package collectionz;
import java.util.*;
class SnakeMatrix
{
static void print(int [][] mat)
{
// Traverse through all rows
for (int i = 0; i < mat.length; i++)
{
// If current row is even, print from
// left to right
if (i % 2 == 0)
{
for (int j = 0; j < mat[0].length; j++)
System.out.print(mat[i][j] +" ");
// If current row is odd, print from
// right to left
}
else
{
for (int j = mat[0].length - 1; j >= 0; j--)
System.out.print(mat[i][j] +" ");
}
}
}
// Driver code
public static void main(String[] args)
{
int mat[][] = new int[][]
{
{ 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 }
};
print(mat);
}
}