Open In App

Print matrix in snake pattern

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 

Matrix printing

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);

Output
45 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)



Next Article
Article Tags :
Practice Tags :

Similar Reads