Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Rotate a Matrix by 180 degrees
To rotate a matrix by 180 degrees, we can achieve this using various approaches. In this article, we'll explore three different methods with step-by-step explanations and complete examples.
A 180-degree rotation of a matrix means that the first row becomes the last row in reverse order, the second row becomes the second-to-last row in reverse order, and so on. Essentially, we're flipping the matrix both horizontally and vertically.
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]]
Visual Representation
Approaches to Rotate a Matrix by 180 Degrees
Here are three different approaches we'll explore:
Using Naive Approach
The naive approach creates a new rotated matrix by traversing the original matrix from the last row to the first row, and within each row, from the last column to the first column.
- Start from the bottom-right corner of the matrix
- Traverse each row from right to left, starting from the bottom row
- Build the rotated matrix row by row
Example
let mat = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
function rotateMatrix(matrix) {
let n = matrix.length;
let rotated = [];
for (let i = n - 1; i >= 0; i--) {
let row = [];
for (let j = n - 1; j >= 0; j--) {
row.push(matrix[i][j]);
}
rotated.push(row);
}
return rotated;
}
console.log("Original Matrix:");
console.log(mat);
console.log("Matrix after 180° rotation:");
console.log(rotateMatrix(mat));
Original Matrix: [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ] Matrix after 180° rotation: [ [ 16, 15, 14, 13 ], [ 12, 11, 10, 9 ], [ 8, 7, 6, 5 ], [ 4, 3, 2, 1 ] ]
Using Reverse and Swap
This approach modifies the matrix in-place by first reversing each row, then swapping rows symmetrically around the center.
- Reverse each row of the matrix individually
- Swap the first row with the last row, second with second-to-last, and so on
- Continue until reaching the middle of the matrix
Step-by-Step Process
Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Step 1: Reverse each row [[3, 2, 1], [6, 5, 4], [9, 8, 7]] Step 2: Swap rows (row 0 with row 2) [[9, 8, 7], [6, 5, 4], [3, 2, 1]] Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Example
let mat = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
function rotateMatrix(matrix) {
let n = matrix.length;
// Step 1: Reverse each row
for (let i = 0; i
Original Matrix:
[ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]
Matrix after 180° rotation:
[
[ 16, 15, 14, 13 ],
[ 12, 11, 10, 9 ],
[ 8, 7, 6, 5 ],
[ 4, 3, 2, 1 ]
]
Using Transpose and Reverse
This approach uses the mathematical property that a 180-degree rotation equals two 90-degree rotations. A 90-degree clockwise rotation can be achieved by transposing the matrix and then reversing each row.
- Transpose the matrix (swap rows and columns)
- Reverse each row
- Repeat both steps once more to complete the 180-degree rotation
Step-by-Step Process
Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Step 1: Transpose [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Step 2: Reverse rows [[7, 4, 1], [8, 5, 2], [9, 6, 3]] Step 3: Transpose again [[7, 8, 9], [4, 5, 6], [1, 2, 3]] Step 4: Reverse rows again [[9, 8, 7], [6, 5, 4], [3, 2, 1]] Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Example
let matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
function transpose(matrix) {
let n = matrix.length;
for (let i = 0; i
Original Matrix:
[ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]
Matrix after 180° rotation:
[
[ 16, 15, 14, 13 ],
[ 12, 11, 10, 9 ],
[ 8, 7, 6, 5 ],
[ 4, 3, 2, 1 ]
]
Comparison
| Approach | Time Complexity | Space Complexity | In-Place |
|---|---|---|---|
| Naive Approach | O(n²) |
