

Int rotateMatrix ( int X, int n ) return temp The last row of the input matrix = The last column of the output matrix in reverse order.The second row of the input matrix = The second column of the output matrix in reverse order and so on.The first row of the input matrix = The first column of the output matrix in reverse order.So one basic idea would be to use an extra matrix of the same size and directly copy elements from the original matrix based on the above observation. The first column in the output matrix: 4, 3, 2, 1.The first row in the input matrix: 1, 2, 3, 4.Similarly, the second, third, … and last rows have turned into their respective columns in reverse order. If we observe the input and output matrices, the following pattern would be visible: The first row has turned into the first column in reverse order. Efficient in-place approach using nested loopsīrute force approach using extra space Solution idea and steps.Efficient in-place approach using transpose of the matrix.Enjoy problem-solving! Discussed solution approaches Important note: Before moving on to solutions, we recommend trying this problem on paper for atleast 15 or 30 minutes. The goal is to perform the matrix rotation in place, meaning we need to rotate the matrix without using extra space. Given an n x n square matrix, write a program to rotate the matrix by 90 degrees in the anti-clockwise direction. Key takeaway: This is a well-known matrix problem that can be solved in-place using loops and matrix properties. Difficulty: Medium, Asked-in: Google, Facebook, Microsoft, Amazon, Morgan Stanley
