Rotate n*n matrix
@sarthaksingh Dry run and check your code.It is generating wrong output even for the sample test case.
It is easy to implement image rotation.For a given nxn matrix You are needed to perform 2 steps:
-
Take transpose along the diagonal.
This you can do by: if(i!=j) then swap(arr[i][j],arr[j][i]) -
Reverse all rows.
This you can do by: swap(arr[i][j],arr[i][n-1-j])
Try to code by yourself using these conditions and you will get it clear.
Note that this is when you want rotation in clockwise direction by 90 degree.
If you want the rotation in anti clockwise direction, you will have to take transpose along the other diagonal.
Just do some index manipulation and swap the elements. Check for the indexes by considering any example and generalize a formula for swapping elements in order to from the transposed matrix.