Javascript Program to Print matrix in snake pattern (original) (raw)
Last Updated : 10 Sep, 2024
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 9
We 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);
`
Output
10 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 matrix
- **Auxiliary space: O(1)
Please refer complete article on Print matrix in snake pattern for more details!
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
- Program to print the arrow pattern Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C++ // C++ program to print the // arrow pattern #in 10 min read
- JavaScript Program to Print Pascal’s Pattern Triangle Pyramid Pascal’s Triangle is a mathematical concept that produces a triangular array of binomial coefficients. It is a series of numbers arranged in the shape of a pyramid, following Pascal's Triangle pattern. Each number in the pyramid is the sum of the two numbers directly above it in the previous row, wi 2 min read
- Programs to print Interesting Patterns Program to print the following pattern: Examples : Input : 5Output:* * * * * * * * * ** * * * * * * ** * * * * ** * * ** ** ** * * ** * * * * ** * * * * * * ** * * * * * * * * *This program is divided into four parts.C++// C++ program to print // the given pattern #include using name 15+ 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
- 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 Rotate Matrix Elements Given a matrix, clockwise rotate elements in it.Examples:Input: 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12The idea is to use loops similar to the program for printing a matrix in spiral form. 5 min read