Javascript Program to Print matrix in snake pattern Last Updated : 10 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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, 6}, {7, 8, 9}};Output : 1 2 3 6 5 4 7 8 9We traverse all rows. For every row, we check if it is even or odd. If even, we print from left to right else print from right to left. JavaScript let M = 4; let N = 4; function print(mat) { let output = ""; // 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++) output += mat[i][j] + " "; // If current row is odd, print from right to left } else { for (let j = N - 1; j >= 0; j--) output += mat[i][j] + " "; } } console.log(output.trim()); } // Driver code let mat = [ [10, 20, 30, 40], [15, 25, 35, 45], [27, 29, 37, 48], [32, 33, 39, 50] ]; print(mat); Output10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Complexity Analysis:Time complexity: O(n^2) for given n*n matrixAuxiliary space: O(1)Please refer complete article on Print matrix in snake pattern for more details! Comment More infoAdvertise with us Next Article Javascript Program to Inplace rotate square matrix by 90 degrees | Set 1 K kartik Follow Improve Article Tags : JavaScript Similar Reads JavaScript Program to Print Matrix in Diagonal Pattern We have given the n*n size matrix and we need to print the elements of this matrix in the diagonal pattern using JavaScript language. Below are the examples for a better understanding of the problem statement. Examples:Input: mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output:1 2 4 7 5 3 6 8 9Expla 4 min read JavaScript Program to Print Given Matrix in Spiral Form Write a JavaScript program to print a given 2D matrix in spiral form. You are given a two-dimensional matrix of integers. Write a program to traverse the matrix starting from the top-left corner which moves right, bottom, left, and up in a spiral pattern until all the elements are visited. Let us un 11 min read Javascript Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16};Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 13, 2 min read Javascript Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3 3 min read Javascript Program to Inplace rotate square matrix by 90 degrees | Set 1 Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 5 min read Javascript Program for Mirror of matrix across diagonal Given a 2-D array of order N x N, print a matrix that is the mirror of the given tree across the diagonal. We need to print the result in a way: swap the values of the triangle above the diagonal with the values of the triangle below it like a mirror image swap. Print the 2-D array obtained in a mat 4 min read Like