Javascript Program to Print matrix in snake pattern Last Updated : 10 Sep, 2024 Comments Improve Suggest changes 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 Print matrix in snake pattern K kartik Follow Improve Article Tags : Matrix JavaScript Web Technologies DSA Practice Tags : Matrix 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 Print matrix in snake pattern 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] 4 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 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 Like