Print matrix in snake pattern
Last Updated :
02 May, 2025
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]]
Output: [1, 2, 4, 3]
Explanation: Printing it in snake pattern will give output as 1 2 4 3.
The approach uses a zigzag pattern to print the elements of the matrix. It traverses each row of the matrix: for even-indexed rows, it prints the elements from left to right, and for odd-indexed rows, it prints the elements from right to left. This zigzag traversal continues for all rows of the matrix, giving the desired output.
C++
#include <iostream>
#include <vector>
using namespace std;
void print(const vector<vector<int>>& mat)
{
int m = mat.size();
int n = mat[0].size();
// Traverse through all rows
for (int i = 0; i < m; i++) {
// If current row is even, print from left to right
if (i % 2 == 0) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
}
// If current row is odd, print from right to left
else {
for (int j = n - 1; j >= 0; j--)
cout << mat[i][j] << " ";
}
}
}
int main()
{
vector<vector<int>> mat = {
{45, 48, 54},
{21, 89, 87},
{70, 78, 15}
};
print(mat);
return 0;
}
Java
import java.util.*;
public class GfG {
static void print(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
// Traverse through all rows
for (int i = 0; i < m; i++) {
// If current row is even, print from left to right
if (i % 2 == 0) {
for (int j = 0; j < n; j++)
System.out.print(mat[i][j] + " ");
} else { // If current row is odd, print from right to left
for (int j = n - 1; j >= 0; j--)
System.out.print(mat[i][j] + " ");
}
}
}
public static void main(String[] args) {
int[][] mat = {
{45, 48, 54},
{21, 89, 87},
{70, 78, 15}
};
print(mat);
}
}
Python
def print(mat):
m = len(mat)
n = len(mat[0])
# Traverse through all rows
for i in range(m):
# If current row is even, print from left to right
if i % 2 == 0:
for j in range(n):
print(mat[i][j], end=' ')
else: # If current row is odd, print from right to left
for j in range(n - 1, -1, -1):
print(mat[i][j], end=' ')
mat = [
[45, 48, 54],
[21, 89, 87],
[70, 78, 15]
]
print(mat)
C#
using System;
using System.Collections.Generic;
class GfG {
static void Print(int[][] mat) {
int m = mat.Length;
int n = mat[0].Length;
// Traverse through all rows
for (int i = 0; i < m; i++) {
// If current row is even, print from left to right
if (i % 2 == 0) {
for (int j = 0; j < n; j++)
Console.Write(mat[i][j] + " ");
} else { // If current row is odd, print from right to left
for (int j = N - 1; j >= 0; j--)
Console.Write(mat[i][j] + " ");
}
}
}
static void Main() {
int[][] mat = {
new int[] {45, 48, 54},
new int[] {21, 89, 87},
new int[] {70, 78, 15}
};
Print(mat);
}
}
JavaScript
function print(mat) {
let m = mat.length;
let n = mat[0].length;
// Traverse through all rows
for (let i = 0; i < m; i++) {
// If current row is even, print from left to right
if (i % 2 === 0) {
for (let j = 0; j < n; j++)
process.stdout.write(mat[i][j] + " ");
}
// If current row is odd, print from right to left
else {
for (let j = n - 1; j >= 0; j--)
process.stdout.write(mat[i][j] + " ");
}
}
}
const mat = [
[45, 48, 54],
[21, 89, 87],
[70, 78, 15]
];
print(mat);
Output45 48 54 87 89 21 70 78 15
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)
Similar Reads
Print matrix in snake pattern from the last column Given a matrix of 2-Dimensional array of n rows and n columns. Print this matrix in snake fashion starting from column n-1 as shown in the figure below . Examples: Input : mat[][] = 1 2 3 4 5 6 7 8 9 Output: 3 2 1 4 5 6 9 8 7 Input: mat[][] = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 3 2 1 5
6 min read
Javascript Program to Print matrix in snake pattern Given 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
2 min read
POTD Solutions | 8 Novâ 23 | Print Matrix in snake Pattern View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Matrix but will also help you build up problem-solving
4 min read
Printing Heart Pattern in C How to print below heart pattern in C? AAAAAAA AAAAAA AAAAAAAAA AAAAAAAA AAAAAAAAAAA AAAAAAAAAA AAAAAAAAAAAAA AAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB
8 min read
Program to print interesting pattern Program to print following pattern: ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** ****5*5*5*5*5**** ***6*6*6*6*6*6*** **7*7*7*7*7*7*7** Examples : Input : 4 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** Input :5 Output : ********1********
9 min read