You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Output:
To rotate a matrix clockwise by 90 degrees, we can utilize an auxiliary matrix. The idea is to iterate through each element of the matrix and place it appropriately in the new matrix. Specifically, each element at position (i, j) in the original matrix will be placed at position (j, n-1-i) in the rotated matrix.
rotated[j][n-1-i] = original[i][j].n is the number of rows (or columns) in the matrix.A more efficient solution is to perform the transformation in place by first transposing the matrix (flipping it over its diagonal) and then reversing each row. Transposing by itself will reorder the elements diagonally, while reversing will yield the required 90-degree clockwise rotation.
(i, j) with the element (j, i).n is the number of rows (or columns) in the matrix.