You are given a square matrix ‘Mat’ of size ‘N’. You need to rotate ‘Mat’ by 90 degrees in the clockwise direction.
Note:
You must rotate the matrix in place, i.e., you must modify the given matrix itself. You must not allocate another square matrix for rotation.
For example
When,
‘N’ = 2 and ‘Mat’ = {{1, 2}, {3, 4}}, we must modify ‘Mat’ to {{3, 1}, {4, 2}}.
The first line contains an integer ‘N’, denoting the size of the matrix ‘Mat’.
Next ‘N’ lines contain ‘N’ integers each, denoting a row of ‘Mat’.
Output Format:
You must modify the given matrix ‘Mat’, such that it is rotated in the clockwise direction by 90 degrees.
1 <= N <= 100
1 <= Mat[i][j] <= 10^9
Time Limit: 1 sec
Try to simulate the rotation process.
Approach:


Algorithm:
O(N * N), where ‘N’ is the size of ‘Mat’.
We are traversing through the entire matrix, which takes O(N * N) time and the swap operations take O(1) time. Hence, the overall time complexity of this solution is O(N * N).
O(1)
Since we are not allocating any extra space anywhere, the overall space complexity of this solution is O(1).