# Print Matrix in a Snake Pattern

Matrix traversal can be trickier than we think, making it a favourite for interviewers to ask questions. We often encounter problems related to 2D matrices and print the elements of a matrix in a specific pattern. One such pattern is the "Snake Pattern".

In this article, we will learn how to write Python, C, C++ and Java programs to print a matrix in a snake pattern.

## What is meant by a Snake Pattern?

The Snake Pattern traversal of a matrix involves moving through the rows alternatingly.

Given a matrix of size N * M, the task is to print the elements of the matrix in the snake pattern.

Steps Involved:

• We start at the top-left corner (0,0) and move right to the end of the row.
• Then, we move to the next row and traverse from right to left.

This process repeats until we cover the entire matrix.

Consider the following example matrix (3 x 3):

The Snake Pattern traversal of this matrix would produce the following output:

Below is the Python Implementation of the Snake Pattern:

Output:

```Matrix in a Snake Pattern:
1 2 3
6 5 4
7 8 9
```

Explanation:

In the above program, we iterate through each row of the matrix.

• For even-indexed rows (0-based indexing), we traverse from left to right,
• And for odd-indexed rows, we traverse from right to left.
• The print() function is used to move to the next line after printing each row.

Time Complexity = O(N*M): Where N*M is the total number of elements in the matrix. The above program visits each element exactly once.

Space Complexity = O(1): No extra space is used.

Below is the C implementation of the Snake Pattern:

Output:

```Matrix in Snake Pattern:
1 2 3
6 5 4
7 8 9
12 11 10
```

Explanation:

In the above program, We call the printMatrixInSnakePattern() function with the matrix (4x3), rows=4, and columns=3 as arguments to print the matrix in a snake pattern.

Inside the function, we use nested loops to iterate through the matrix in the same way as the Python code. We use 'printf' to print the elements and '\n' to move to the next line after each row.

Below is the C++ implementation of the Snake Pattern:

Output:

```Matrix in the Snake Pattern:
1 2 3 4
8 7 6 5
9 10 11 12
```

Explanation:

In the above program, We call the printMatrixInSnakePattern() function with the matrix (3x4), rows=3, and columns=4 as arguments to print the matrix in a snake pattern.

Inside the function, we use nested loops to iterate through the matrix in the same way as the Python and C codes. We use 'cout' to print the elements and 'endl' to move to the next line after each row.

Below is the Java Implementation of the Snake Pattern:

Output:

```Matrix in Snake Pattern:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
```

Explanation:

In the above program, we created a class called SnakePatternMatrix. Inside the class, we define a printMatrixInSnakePattern method that prints the elements of the given matrix in a snake pattern.

Inside the method, we have used nested loops to iterate through the matrix in the same way as the Python, C, or C++ code. We use System.out.print to print the elements and System.out.println to move to the next line after each row.

## Conclusion:

In this article, we provided simple Python, C, C++ and Java codes to print a matrix in a snake pattern. You can use any code you are familiar with and try to pass matrices of different dimensions. But make sure when using C or C++ code you need to specify the number of columns when defining the function's arguments.