JavaScript Program for Rotate a Matrix by 180 degrees


A square matrix is a kind of 2-D array with an equal number of rows and columns and we have to rotate the matrix by 180 degrees anticlockwise. Rotating a matrix anti-clockwise means first converting all the rows into columns and the first row will be the first column and then again rotating the rows into columns and the first row will be the first column and so on.

Let’s see an example −

Input 1:
N = 3
Matrix = [ [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9] ]
Output 1:
Matrix = [ [9, 8 ,7],
   [6, 5, 4],
   [3, 2, 1] ]

Input 2:
N = 5
Matrix = [ [1, 2, 3, 4, 5],
   [6, 7, 8, 9, 10],
   [11, 12, 13, 14, 15],
   [16, 17, 18, 19, 20],
   [21, 22, 23, 24, 25] ]
Output 2:
Matrix = [ [25, 24, 23, 22, 21],
   [20, 19, 18, 17, 16],
   [15, 14, 13, 12, 11],
   [10, 9, 8, 7, 6],
   [5, 4, 3, 2, 1] ]

Naive Approach

Let’s see an example of it

N = 3;
Matrix = [ [00, 01, 02],
   [10, 11, 12],
   [20, 21, 22] ]

We Rotated the matrix by 90 degrees (first time)

Matrix become = [ [02, 12, 22],
   [01, 11, 21],
   [00, 10, 20] ]

Again Rotated the matrix by 90 degree (second time so it becomes 90+90=180 degree)

Matrix become = [ [22, 21, 20],
   [12, 11, 10],
   [02, 01, 00] ]

So basically observe here if we traverse the row of matrices from n-1 to 0 (included) and then in a nested forloop we traverse the column n-1 to 0 (included) we get the rotated matrix. For better understanding lets see the code of this approach below.

Example

//function for rotate the matrix 
function rotateMatrix( matrix){
   // getting size of the matrix 
   var n = matrix.length;
   // start getting elements for the last row 
   for(var i = n-1; i >= 0; i--){
      var temp = "";
      // start getting elements for the last column
      for(var j = n-1; j >= 0; j--){
         temp += matrix[i][j] + " ";
      }
      console.log(temp);
   }
}
// defining the matrix 
var n = 4;
var mat = [ [ 1, 2, 3, 4 ],
   [ 5, 6, 7, 8 ],
   [ 9, 10, 11, 12],
   [ 13, 14, 15, 16]];
console.log("Matrix after rotation is: ")
//calling the function
rotateMatrix(mat);

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the number of rows or columns of the given matrix, as we are traversing over it a single time.

The space complexity of the above code is O(1), as haven’t used any extra space here.

Storing Result in Given Matrix

In this approach we are going to store the result in the matrix we have and we are not going to use any extra space. As we can see that the first and the last row are swapped but in the reverse order. Similarly second and second last row are swap in opposite manner. So by this observation we can write the below code −

Example

//function for rotate the matrix 
function rotateMatrix( matrix){
   // getting size of the matrix 
   var n = matrix.length;
   // start getting elements for the last row 
   for(var i = 0; i < n/2; i++){
      matrix[i].reverse();
      matrix[n-1-i].reverse();
      for(var j = 0; j<n;j++) {
         [matrix[i][j], matrix[n-1-i][j]] = [matrix[n-1-i][j], matrix[i][j]]
      }
   }
   console.log(matrix)
}
// defining the matrix 
var n = 4;
var mat = [ [ 1, 2, 3, 4 ],
   [ 5, 6, 7, 8 ],
   [ 9, 10, 11, 12],
   [ 13, 14, 15, 16]];
console.log("Input Matrix:
", mat) console.log("Matrix after rotation is: ") //calling the function rotateMatrix(mat);

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the number of rows or columns of the given matrix, as we are traversing over it a single time.

The space complexity of the above code is O(1), as haven’t used any extra space here.

Conclusion

In this tutorial, we have seen two different approaches to rotating a given matrix by 180 degrees. By the observations, we concluded that the elements of the reversed first row will be swapped with the elements of the reversed last row. Similarly, for the second row and second last row and so on. We have implemented codes with the time complexity of the O(N*N) and without using any extra space.

Updated on: 12-Apr-2023

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements