Given an n x n matrix. In the given matrix, you have to print the elements of the matrix in the snake pattern.
Examples :
Input: mat[][] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50}};
Output: 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32

Input: mat[][] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
Output: 1 2 3 6 5 4 7 8 9
Approach: Follow the steps below to solve the problem:
- Traverse all rows.
- For every row, check if it is even or odd.
- If even, we print from left to right
- else print from right to left.
Below is the implementation of above approach:
C++
#include <iostream>
#define M 4
#define N 4
using namespace std;
void print( int mat[M][N])
{
for ( int i = 0; i < M; i++) {
if (i % 2 == 0) {
for ( int j = 0; j < N; j++)
cout << mat[i][j] << " " ;
}
else {
for ( int j = N - 1; j >= 0; j--)
cout << mat[i][j] << " " ;
}
}
}
int main()
{
int mat[M][N] = { { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
print(mat);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void print( int [][] mat)
{
for ( int i = 0 ; i < mat.length; i++) {
if (i % 2 == 0 ) {
for ( int j = 0 ; j < mat[ 0 ].length; j++)
System.out.print(mat[i][j] + " " );
}
else {
for ( int j = mat[ 0 ].length - 1 ; j >= 0 ; j--)
System.out.print(mat[i][j] + " " );
}
}
}
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);
}
}
|
Python3
M = 4
N = 4
def printf(mat):
global M, N
for i in range (M):
if i % 2 = = 0 :
for j in range (N):
print ( str (mat[i][j]),
end = " " )
else :
for j in range (N - 1 , - 1 , - 1 ):
print ( str (mat[i][j]),
end = " " )
mat = [[ 10 , 20 , 30 , 40 ],
[ 15 , 25 , 35 , 45 ],
[ 27 , 29 , 37 , 48 ],
[ 32 , 33 , 39 , 50 ]]
printf(mat)
|
C#
using System;
class GFG {
static void print( int [, ] mat)
{
for ( int i = 0; i < mat.GetLength(0); i++) {
if (i % 2 == 0) {
for ( int j = 0; j < mat.GetLength(1); j++)
Console.Write(mat[i, j] + " " );
}
else {
for ( int j = mat.GetLength(1) - 1; j >= 0;
j--)
Console.Write(mat[i, j] + " " );
}
}
}
public static void Main()
{
int [, ] mat = { { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
print(mat);
}
}
|
PHP
<?php
$M = 4;
$N =4;
function printLN( $mat )
{
global $M ;
global $N ;
for ( $i = 0; $i < $M ; $i ++)
{
if ( $i % 2 == 0)
{
for ( $j = 0; $j < $N ; $j ++)
echo $mat [ $i ][ $j ], " " ;
}
else
{
for ( $j = $N - 1; $j >= 0; $j --)
echo $mat [ $i ][ $j ] , " " ;
}
}
}
$mat = array ( array (10, 20, 30, 40),
array (15, 25, 35, 45),
array (27, 29, 37, 48),
array (32, 33, 39, 50));
printLN( $mat );
?>
|
Javascript
<script>
let M = 4;
let N = 4;
function print(mat)
{
for (let i = 0; i < M; i++)
{
if (i % 2 == 0)
{
for (let j = 0; j < N; j++)
document.write(mat[i][j] + " " );
}
else
{
for (let j = N - 1; j >= 0; j--)
document.write(mat[i][j] + " " );
}
}
}
let mat = [ [ 10, 20, 30, 40 ],
[ 15, 25, 35, 45 ],
[ 27, 29, 37, 48 ],
[ 32, 33, 39, 50 ] ];
print(mat);
</script>
|
Output
10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32
Time Complexity: O(N x M), Traversing over all the elements of the matrix, therefore N X M elements are there.
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Sep, 2022
Like Article
Save Article