
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
Rotate a Matrix by 180 Degrees in JavaScript
To rotate a matrix by 180 degrees, can be achieved using various approaches. We have discussed three different approaches in this article with stepwise explanation of codes and examples.
In this article, we are having a 2D square matrix, our task is to write a JavaScript program to rotate a matrix by 180 degrees.
Example
Input: matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] Output: [[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]]
Approaches to Rotate a Matrix by 180 degrees
Here is a list of approaches to rotate a matrix by 180 degrees which we will be discussing in this article with stepwise explanation and complete example codes.
Using Naive Approach
To rotate a matrix by 180 degrees, we have used naive approach where we have traversed the rows and column starting from end to the start of the matrix, reversing the rows and columns.
- First, we have declared a 2D matrix and length of the matrix. Then we have defined a function rotateMatrix() which accepts matrix as argument.
- We have used nested for loop where outer for loop traverse from last row to first row reversing the matrix and inner for loop traverse from last column to starting column reversing the column.
- The reversed matrix is then appended to temp and this temp is then printed in web console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps to rotate a matrix by 180 degrees using naive approach.
let mat = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]; let n = 4; function rotateMatrix(matrix) { let n = matrix.length; for (let i = n - 1; i >= 0; i--) { let temp = ""; for (let j = n - 1; j >= 0; j--) { temp += matrix[i][j] + " "; } console.log(temp); } } console.log("Matrix after rotation: ") rotateMatrix(mat);
Using Reverse and Swap
In this approach to rotate a matrix by 180 degrees, we have used reversing the elements of rows and then swapping the element of a row with other row.
- First, we have declared a 2D matrix and length of the matrix. Then we have defined a function rotateMatrix which accepts matrix as argument.
- In the function rotateMatrix, we have used a nested for loop where outer for loop iterates over rows and inner for loop iterates over the column.
- The reverse() function reverses the elements of the rows. In the inner loop, elements of the rows are swapped. The rows are decided by i and n-1-i. For eg: elements of row 0 and elements of row 3 are swapped in first iteration.
- After the completion of loop, the rotated matrix is printed in web console using console.log() method.
Solution
Input: [[1,2, 3], [4, 5, 6], [7, 8, 9]] Step 1: Reverse the rows [for i=0, n-1-i=2] [[3, 2, 1], [4, 5, 6], [9, 8, 7]] Step 2: Swap the elements of rows for (mat[i][j], mat[n-1-i][j]) => mat[0][0] = mat[2][0] mat[0][1] = mat[2][1] mat[0][2] = mat[2][2] =>[[9, 8, 7], [4, 5, 6], [3, 2, 1]] Step 3: Repeat the above steps till i Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Example
Here is a complete example code implementing above mentioned steps to rotate a matrix by 180 degrees using swapping and reversing the row.
let mat = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]; let n = 4; console.log("Input Matrix:", mat) function rotateMatrix(matrix) { let n = matrix.length; for (let i = 0; iUsing Transpose and Reverse
In this approach first we find the transpose of the matrix and then reverse the elements of row. This process rotates the matrix by 90 degrees. So we repeat these two process once again to rotate 180 degrees.
- First, we have declared a 2D matrix and defined three functions transpose(), reverse() and rotate(). All the three functions accepts matrix as argument.
- We have used transpose() function to find the transpose of the matrix. We have used nested for loop for iteration and element swapping.
- Then, we have used reverse() function to reverse the element of rows after finding the transpose.
- We will use transpose and reverse one more time to rotate the matrix by 180degree.
- Then we have used rotate() function to call the transpose() and reverse() function and after rotation the matrix is returned.
- The rotated matrix is then displayed in web console using console.log() method.
Solution
Input: Matrix: [[1,2, 3], [4, 5, 6], [7, 8, 9]] Transpose(matrix): [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Reverse(Transpose(matrix)): [[7, 4, 1], [8, 5, 2], [9, 6, 3]] Transpose: [[7, 8, 9], [4, 5, 6], [1, 2, 3]] Reverse(Transpose): [[9, 8, 7], [6, 5, 4], [3, 2, 1]] Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Example
Here is a complete example code implementing above mentioned steps to rotate a matrix by 180 degrees by finding transpose and reversing.
let matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; function transpose(matrix) { let n = matrix.length; for (let i = 0; iComplexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach Time Complexity Space Complexity Naive approach O(n^2) O(1) Reverse and Swap O(n^2) O(1) Transpose and Reverse O(n^2) 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.