Striver's SDE Sheet Journey - #7 Rotate Image
Tags: beginners, programming, dsa, java
Published: 2021-12-27 14:55:23

> **<u>Problem Statement</u> :-**

_You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)_.

**Example 1:**

```
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
```


![Rotate Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lofcldj8bhbjtvwp74h.png)

after rotating by 90 degree,

![Rotate Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8c3estzslg0tt7hobte.png)

##  <u>Solution 1</u>
_from the above images, you can see, after the rotation, the first row of the matrix has turned into the last column of the matrix and the second row of the matrix has turned into the second last column, and so on._

_so what we are going to do is, initialize one more matrix `rotated` and copy the first row from the original matrix to the last column of the new matrix `rotated`, and so on._

**step-1** initialize a n*n size of matrix `rotate`.

**step-2** run a loop from `i=0` to `n`

>  **1.** run another loop from `j=0` to `n`.
>  >  **2.** copy `matrix` values to the last column of `rotate` matrix.
  `rotated[j][n-i-1] = matrix[i][j]`

**step-3** end.


> Java

```java
class Solution {
    public void rotate(int[][] matrix) {
        
        int n = matrix.length;  
        int rotated[][] = new int[n][n];
        
        for (int i = 0; i < n; i++) {
            
            for (int j = 0; j < n; j++) {
                
                rotated[j][n - i - 1] = matrix[i][j];
            }
        }
        
        
        for(int i=0; i<n;i++)
        System.out.println(Arrays.toString(rotated[i]));
    }
}
```
**_Time Complexity_** : **O(n*n)**,bcoz we are running two loops.

**_space Complexity_** : **O(n*n)**,initialising a new n*n matrix.

##  <u>Solution 2</u>
_if you observed the first row of the output, is nothing, just the reverse of the first column._

_for the expected output, first, we have to transpose the matrix and then reverse each row of the matrix._

lets understand it in a visual way.

![rotate ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qof0g0zm429gjuholgqd.png)
**step-1** transpose the matrix. (changing rows to columns & columns to rows)

 
![rotate image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppkoqaypio2lscnak7qb.png)

**step-2** reverse each row of the matrix.
 
![rotate image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p4qvslpalaugy0u0qm8.png)

> Java

```java
class Solution {
    public void rotate(int[][] matrix) {
        
        int n = matrix.length;
      
        // transpose the matrix
       for(int i=0; i< n;i++){
            
            for(int j=0; j<i; j++){
                
               swap(matrix,i,j);
            }
        }
        
    
        // reverse rows
        for(int i=0; i<n;i++){
            
           reverse(matrix[i],n);
        }
        
    }
    
    private void swap(int[][] matrix,int i, int j){
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
    }
    
    private void reverse(int[] row,int n){
        
        for(int i=0; i<n/2;i++){
            int temp = row[i];
            row[i] = row[n-i-1];
            row[n-i-1] = temp;
        }
    }
    
  
}
```

**_Time Complexity_** : transposing the matrix + reversing each row of the matrix,then **O(n*n) + O(n*n)**.

**_space Complexity_** : in this case we are not using any extra space,then **O(1)**.

**T**hank you for reading this article I hope I explained it clearly.