JavaScript Program to Print Matrix in Diagonal Pattern
Last Updated :
05 Dec, 2023
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 9
Explanation:
Start from 1
Then from upward to downward diagonally i.e. 2 and 4
Then from downward to upward diagonally i.e 7, 5, 3
Then from up to down diagonally i.e 6, 8
Then down to up i.e. end at 9.Input: mat[4][4] = {{1, 2, 3, 10},
{4, 5, 6, 11},
{7, 8, 9, 12},
{13, 14, 15, 16}}
Output: 1 2 4 7 5 3 10 6 8 13 14 9 11 12 15 16
Explanation : Start from 1 Then from upward to downward diagonally i.e. 2 and 4
Then from downward to upward diagonally i.e 7, 5, 3
Then from upward to downward diagonally i.e. 10 6 8 13
Then from downward to upward diagonally i.e 14 9 11
Then from upward to downward diagonally i.e. 12 15 then end at 16 .
These are the following approaches to solve this problem:
Approach 1: Using Nested For Loops
- In this approach, we use the nested for loops where we are iterating over the diagonals of the matrix by alternating between upside and downside directions.
- We use the nested loops to go through the diagonals by adjusting the positions to ensure that the correct traversal is done in both row and column and then we print the elements in the diagonal pattern.
Example: In this example, we print Matrix in Diagonal Pattern in JavaScript using Nested For Loops.
JavaScript
// Input matrix
let mat = [
[1, 2, 3, 10],
[4, 5, 6, 11],
[7, 8, 9, 12],
[13, 14, 15, 16]
];
// Getting the number of rows
// and columns in the matrix
let rowLen = mat.length;
let colLen = mat[0].length;
// Looping through diagonals
for (let i = 0; i < rowLen + colLen - 1; i++) {
// Checking if current
// diagonal is even or odd
if (i % 2 === 0) {
// Looping through elements in
// the current diagonal (even)
for (let j = Math.min(i, rowLen - 1); j >= 0
&& i - j < colLen; j--) {
// Print the element in
// the current diagonal
console.log(mat[j][i - j]);
}
} else {
// Looping through elements in
// the current diagonal (odd)
for (let j = Math.min(i, colLen - 1); j >= 0
&& i - j < rowLen; j--) {
// Printing the element
// in the current diagonal
console.log(mat[i - j][j]);
}
}
}
Output1
2
4
7
5
3
10
6
8
13
14
9
11
12
15
16
Approach 2: Using ZigZag Iteration
- In this approach, we are using the zigzag iteration to go through the matrix diagonally, we are using the if-else blocks to control the direction of traversal and then coming to upside and downside along with diagonals.
- The up variable here is used between the upside and downside movements which also makes sure the complete zigzag traversal.
Example: In this example, we print Matrix in a Diagonal Pattern in JavaScript using ZigZag Iteration.
JavaScript
// Input matrix
let mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
// Initialize variables for row,
// column, matrix dimensions, and direction
let r = 0, c = 0,
rowLen = mat.length,
numLen = mat[0].length,
up = true;
// Loop through each element in the matrix
for (let i = 0; i < rowLen * numLen; i++) {
// Print the current element
console.log(mat[r][c]);
// Check the direction (up or down)
// bfor the next element
if (up) {
// Move diagonally up if possible,
// otherwise change direction
if (r > 0 && c < numLen - 1) {
r--;
c++;
} else {
up = false;
// If still within the column boundaries,
// move right; otherwise, move down
if (c < numLen - 1) {
c++;
} else {
r++;
}
}
} else {
// Move diagonally down if possible,
// otherwise change direction
if (c > 0 && r < rowLen - 1) {
c--;
r++;
} else {
up = true;
// If still within the row boundaries,
// move down; otherwise, move right
if (r < rowLen - 1) {
r++;
} else {
c++;
}
}
}
}
Reference: https://www.geeksforgeeks.org/print-matrix-diagonal-pattern/
Similar Reads
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
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 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
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