
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Program for Mirror of Matrix Across Diagonal
To write a javascript program for mirror of matrix across diagonal, we will be discussing two approaches with their code example and explanation of code.
In this article we are having a 2D matrix. Our task is to write a JavaScript program for mirror of matrix across diagonal.
Example
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Approaches for Mirror of Matrix Across Diagonal
Here is a list of approaches to change the position of the scrollbar using CSS which we will be discussing in this article with stepwise explanation and complete example codes.
Using a New Matrix
For mirror of matrix across diagonal we have used a new matrix in this approach to store the mirrored elements of original matrix.
- We have declares a 2D matrix mat and defined a function mirror() that accepts mat as argument.
- We have got the matrix size using length property and declared an empty matrix mirrorMatrix where we will store the mirrored matrix elements.
- Using nested for loop, we traverse the matrix. We store the ith row and jth column of matrix in jth row and ith column of mirrorMatrix creating a mirror image across diagonal.
- As the loop ends mirrorMatrix is returned and the result is displayed in web console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps for mirror of matrix across diagonal using a new matrix.
let mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; console.log("Original matrix is:"); console.log(mat); function mirror(mat) { let n = mat.length; let mirrorMatrix = []; for (let i = 0; i < n; i++) { mirrorMatrix[i] = []; for (let j = 0; j < n; j++) { mirrorMatrix[i][j] = mat[j][i]; } } return mirrorMatrix; } console.log("Mirror of the matrix across diagonal is:"); console.log(mirror(mat));
The output of the above code is as follows:
Original matrix is: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Mirror of the matrix across diagonal is: [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
Using Swapping
In this approach we have used swapping of elements. We have ith row with jth column and jth column with ith row.
- We have declares a 2D matrix mat and defined a function mirror that accepts mat as argument.
- We have got the matrix size using length property and declared an empty matrix mirrorMatrix where we will store the mirrored matrix elements.
- Then we have used nested for loop to traverse the matrix and swap the value of mat[i][j] with mat[j][i].
- Using this we can create mirror of matrix without creating a new matrix.
- As the loop ends mat is returned and the result is displayed in web console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps for mirror of matrix across diagonal by swapping.
let mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; console.log("Original matrix is:"); console.log(mat); function mirror(mat) { let n = mat.length; for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { let temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp; } } return mat; } console.log("Mirror of the matrix across diagonal is:"); console.log(mirror(mat));
The output of the above code is as follows"
Original matrix is: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Mirror of the matrix across diagonal is: [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Using a New Matrix | O(n^2) | O(n) |
Using Swapping | O(n^2) | O(1) |
In this article we discussed two different approaches to write a javascript program for mirror of matrix across diagonal. These approaches are: by using a new matrix and by swapping. Both the approaches has the time complexity of O(n^2) but the second approach is space efficient as it has space complexity of O(1).
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.