# Rotate a matrix by 90 degree in clockwise direction without using any extra space

Given a square matrix, turn it by 90 degrees in clockwise direction without using any extra space.

Examples:

```Input:
1 2 3
4 5 6
7 8 9
Output:
7 4 1
8 5 2
9 6 3

Input:
1 2
3 4
Output:
3 1
4 2
```

Method 1

Approach: The approach is similar to Inplace rotate square matrix by 90 degrees | Set 1. The only thing that is different is to print the elements of cycle in clockwise direction i.e. An N x N matrix will have floor(N/2) square cycles.
For example, a 3 X 3 matrix will have 1 cycle. The cycle is formed by its 1st row, last column, last row, and 1st column.
For each square cycle, we swap the elements involved with the corresponding cell in the matrix in the clockwise direction. We just need a temporary variable for this.

Explanation:

Let size of row and column be 3.
During first iteration –
a[i][j] = Element at first index (leftmost corner top)= 1.
a[j][n-1-i]= Rightmost corner top Element = 3.
a[n-1-i][n-1-j] = Righmost corner bottom element = 9.
a[n-1-j][i] = Leftmost corner bottom element = 7.
Move these elements in the clockwise direction.
During second iteration –
a[i][j] = 2.
a[j][n-1-j] = 6.
a[n-1-i][n-1-j] = 8.
a[n-1-j][i] = 4.
Similarly, move these elements in the clockwise direction.

Below is the implementation of above approach:

 `// C++ implementation of above approach` `#include ` `using` `namespace` `std;`   `#define N 4`   `// Function to rotate the matrix 90 degree clockwise` `void` `rotate90Clockwise(``int` `a[N][N])` `{`   `    ``// Traverse each cycle` `    ``for` `(``int` `i = 0; i < N / 2; i++) {` `        ``for` `(``int` `j = i; j < N - i - 1; j++) {`   `            ``// Swap elements of each cycle` `            ``// in clockwise direction` `            ``int` `temp = a[i][j];` `            ``a[i][j] = a[N - 1 - j][i];` `            ``a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j];` `            ``a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i];` `            ``a[j][N - 1 - i] = temp;` `        ``}` `    ``}` `}`   `// Function for print matrix` `void` `printMatrix(``int` `arr[N][N])` `{` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``for` `(``int` `j = 0; j < N; j++)` `            ``cout << arr[i][j] << ``" "``;` `        ``cout << ``'\n'``;` `    ``}` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[N][N] = { { 1, 2, 3, 4 },` `                      ``{ 5, 6, 7, 8 },` `                      ``{ 9, 10, 11, 12 },` `                      ``{ 13, 14, 15, 16 } };` `    ``rotate90Clockwise(arr);` `    ``printMatrix(arr);` `    ``return` `0;` `}`

 `// Java implementation of above approach` `import` `java.io.*;`   `class` `GFG ` `{` `    `  `static` `int` `N = ``4``;`   `// Function to rotate the matrix 90 degree clockwise` `static` `void` `rotate90Clockwise(``int` `a[][])` `{`   `    ``// Traverse each cycle` `    ``for` `(``int` `i = ``0``; i < N / ``2``; i++)` `    ``{` `        ``for` `(``int` `j = i; j < N - i - ``1``; j++)` `        ``{`   `            ``// Swap elements of each cycle` `            ``// in clockwise direction` `            ``int` `temp = a[i][j];` `            ``a[i][j] = a[N - ``1` `- j][i];` `            ``a[N - ``1` `- j][i] = a[N - ``1` `- i][N - ``1` `- j];` `            ``a[N - ``1` `- i][N - ``1` `- j] = a[j][N - ``1` `- i];` `            ``a[j][N - ``1` `- i] = temp;` `        ``}` `    ``}` `}`   `// Function for print matrix` `static` `void` `printMatrix(``int` `arr[][])` `{` `    ``for` `(``int` `i = ``0``; i < N; i++)` `    ``{` `        ``for` `(``int` `j = ``0``; j < N; j++)` `        ``System.out.print( arr[i][j] + ``" "``);` `        ``System.out.println();` `    ``}` `}`   `// Driver code`   `    ``public` `static` `void` `main (String[] args) ` `    ``{` `            ``int` `arr[][] = { { ``1``, ``2``, ``3``, ``4` `},` `                      ``{ ``5``, ``6``, ``7``, ``8` `},` `                      ``{ ``9``, ``10``, ``11``, ``12` `},` `                      ``{ ``13``, ``14``, ``15``, ``16` `} };` `    ``rotate90Clockwise(arr);` `    ``printMatrix(arr);` `    ``}` `}`   `// This code has been contributed by inder_verma.`

 `# Function to rotate the matrix` `# 90 degree clockwise` `def` `rotate90Clockwise(A):` `    ``N ``=` `len``(A[``0``])` `    ``for` `i ``in` `range``(N ``/``/` `2``):` `        ``for` `j ``in` `range``(i, N ``-` `i ``-` `1``):` `            ``temp ``=` `A[i][j]` `            ``A[i][j] ``=` `A[N ``-` `1` `-` `j][i]` `            ``A[N ``-` `1` `-` `j][i] ``=` `A[N ``-` `1` `-` `i][N ``-` `1` `-` `j]` `            ``A[N ``-` `1` `-` `i][N ``-` `1` `-` `j] ``=` `A[j][N ``-` `1` `-` `i]` `            ``A[j][N ``-` `1` `-` `i] ``=` `temp`   `# Function to print the matrix` `def` `printMatrix(A):` `    ``N ``=` `len``(A[``0``])` `    ``for` `i ``in` `range``(N):` `        ``print``(A[i])`   `# Driver code` `A ``=` `[[``1``, ``2``, ``3``, ``4``],` `     ``[``5``, ``6``, ``7``, ``8``], ` `     ``[``9``, ``10``, ``11``, ``12``], ` `     ``[``13``, ``14``, ``15``, ``16``]]` `rotate90Clockwise(A)` `printMatrix(A)`   `# This code was contributed ` `# by pk_tautolo`

 `// C# implementation of above approach` `using` `System; `   `class` `GFG ` `{` `static` `int` `N = 4;`   `// Function to rotate the matrix ` `// 90 degree clockwise` `static` `void` `rotate90Clockwise(``int``[,] a)` `{`   `    ``// Traverse each cycle` `    ``for` `(``int` `i = 0; i < N / 2; i++)` `    ``{` `        ``for` `(``int` `j = i; j < N - i - 1; j++)` `        ``{`   `            ``// Swap elements of each cycle` `            ``// in clockwise direction` `            ``int` `temp = a[i, j];` `            ``a[i, j] = a[N - 1 - j, i];` `            ``a[N - 1 - j, i] = a[N - 1 - i, N - 1 - j];` `            ``a[N - 1 - i, N - 1 - j] = a[j, N - 1 - i];` `            ``a[j, N - 1 - i] = temp;` `        ``}` `    ``}` `}`   `// Function for print matrix` `static` `void` `printMatrix(``int``[,] arr)` `{` `    ``for` `(``int` `i = 0; i < N; i++)` `    ``{` `        ``for` `(``int` `j = 0; j < N; j++)` `        ``Console.Write( arr[i, j] + ``" "``);` `        ``Console.Write(``"\n"``);` `    ``}` `}`   `// Driver code` `public` `static` `void` `Main () ` `    ``{` `    ``int` `[,]arr = {{1, 2, 3, 4},` `                  ``{5, 6, 7, 8},` `                  ``{9, 10, 11, 12},` `                  ``{13, 14, 15, 16}};` `    ``rotate90Clockwise(arr);` `    ``printMatrix(arr);` `}` `}`   `// This code is contributed ` `// by ChitraNayal`

 ``

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

Method 2:

Approach: The approach is based on the pattern made by indices after rotating the matrix. Consider the following illustration to have a clear insight into it.

Consider a 3 x 3 matrix having indices (i, j) as follows.

00 01 02
10 11 12
20 21 22

After rotating the matrix by 90 degrees in clockwise direction, indices transform into
20 10 00  current_row_index = 0, i = 2, 1, 0
21 11 01 current_row_index = 1, i = 2, 1, 0
22 12 02  current_row_index = 2, i = 2, 1, 0

Observation: In any row, for every decreasing row index i, there exists a constant column index j, such that j = current_row_index

This pattern can be printed using 2 nested loops.

(This pattern of writing indices is achieved by writing the exact indices of the desired elements of
where they actually existed in the original array.)

Below is the implementation of the above approach:

 `// C++ implementation of above approach` `#include ` `using` `namespace` `std;`   `#define N 4`   `// Function to rotate the matrix 90 degree clockwise` `void` `rotate90Clockwise(``int` `arr[N][N])` `{` `    ``// printing the matrix on the basis of` `    ``// observations made on indices.` `    ``for` `(``int` `j = 0; j < N; j++) ` `    ``{` `        ``for` `(``int` `i = N - 1; i >= 0; i--)` `            ``cout << arr[i][j] << ``" "``;` `        ``cout << ``'\n'``;` `    ``}` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[N][N] = { { 1, 2, 3, 4 },` `                      ``{ 5, 6, 7, 8 },` `                      ``{ 9, 10, 11, 12 },` `                      ``{ 13, 14, 15, 16 } };` `    ``rotate90Clockwise(arr);` `    ``return` `0;` `}`   `// This code is contributed by yashbeersingh42`

 `// Java implementation of above approach` `import` `java.io.*;`   `class` `GFG {` `    ``static` `int` `N = ``4``;`   `    ``// Function to rotate the matrix 90 degree clockwise` `    ``static` `void` `rotate90Clockwise(``int` `arr[][])` `    ``{` `        ``// printing the matrix on the basis of` `        ``// observations made on indices.` `        ``for` `(``int` `j = ``0``; j < N; j++)` `        ``{` `            ``for` `(``int` `i = N - ``1``; i >= ``0``; i--)` `                ``System.out.print(arr[i][j] + ``" "``);` `            ``System.out.println();` `        ``}` `    ``}` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `arr[][] = { { ``1``, ``2``, ``3``, ``4` `},` `                        ``{ ``5``, ``6``, ``7``, ``8` `},` `                        ``{ ``9``, ``10``, ``11``, ``12` `},` `                        ``{ ``13``, ``14``, ``15``, ``16` `} };` `        ``rotate90Clockwise(arr);` `    ``}` `}` `// This code is contributed by yashbeersingh42`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :