# Rotate Matrix

Given a 2 dimensional array that looks like an NxN matrix, where each slot has a integer, write a method to rotate the matrix to the right. (can you do this in place?)

```
int[][] exampleMatrix = {
{1,2,3},
{4,5,6},
{7,8,9},
};
// all the items have been rotated from left to right
int[][] exampleResponse = {
{7,4,1},
{8,2,6},
{9,6,3}
};
```

## Link here to the repo to solve the problem

π## π Tips

Try thinking about it layer by layer. Can you rotate a specific layer?

Rotating a specific layer would just mean swapping the values in four arrays. If you were asked to swap the values in two arrays, could you do this? Can you then extend it to four arrays?

## π Solution 1

I've implemented a simple solution which uses another matrix as a placeholder in order to append the numbers over which im iterating through the matrix. This solution will take O(N^2).

```
int[][] solution(int[][] matrix) {
int[][] result = {
{0,0,0},
{0,0,0},
{0,0,0}
};
for (int i = 0; i < matrix.length; i++) {
int c = 0;
for (int j = matrix.length - 1; j >= 0; j--) {
int current = matrix[i][j];
result[c][i] = current;
c++;
}
c = 0;
}
return result;
}
```

This solution is also taking O(N^2) space, could you find a way of reducing the space to O(1) by doing the replacement in place?

*Question added by Diego Romero*