In-place rotate the matrix by 180 degrees

Given a square matrix, rotate the matrix by 180 degrees in clock-wise direction. The transformation should be done in-place in quadratic time.


For example,


Input:
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

 


 

If we swap elements of first row with the elements of last row in reverse order, elements of second row with the elements of second last row in reverse order, and so on.. we will get our desired matrix.

C++

Download   Run Code

Output:

16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1

 
The time complexity of above solution is O(N2) and auxiliary space used by the program is O(1).

 
Related Posts: In-place rotate matrix by 90 degrees in clock-wise direction

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz