Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

Given a 2-dimensional matrix of size ‘N’ x ‘M’, rotate the elements of the matrix clockwise.

```
For example:
Input Matrix: [ [ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ] ]
Output Matrix: [ [ 4, 1, 2 ]
[ 7, 5, 3 ]
[ 8, 9, 6 ] ]
The output matrix is generated by rotating the elements of the input matrix in a clockwise direction. Note that every element is rotated only once.
```

```
You do not need to print anything; it has already been taken care of. Also, update the given matrix in-place.
```

Detailed explanation

```
1 <= T <= 10
1 <= N, M <= 100
-10^5 <= data <= 10^5,
where ‘T’ is the number of test cases, ‘N’ and ‘M’ are the numbers of rows and columns respectively and ‘data’ is the value of the elements of the matrix.
```

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

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

```
The resulting matrix after rotating the given matrix clockwise is shown above.
```

```
2
2 2
1 3
4 5
3 3
3 4 5
5 6 7
8 10 20
```

```
4 1
5 3
5 3 4
8 6 5
10 20 7
```