Rotate matrix
Rotate N x N matrix in 90 degrees, in place.
void rotate90(int** Mat, int N) { bool* visited = new bool[N*N]; memset(visited, 0, sizeof(bool) * N * N); for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { //Mat[row][col] -> Mat[col][N-1-row] //Mat[col][N-1-row] -> Mat[N-1-row][N-1-col] //Mat[N-1-row][N-1-col] -> Mat[N-1-col][row] //Mat[N-1-col][row] -> Mat[row][col] if (visited[row*N + col]) continue; int temp = Mat[row][col]; Mat[row][col] = Mat[N - 1 - col][row]; Mat[N - 1 - col][row] = Mat[N - 1 - row][N - 1 - col]; Mat[N - 1 - row][N - 1 - col] = Mat[col][N - 1 - row]; Mat[col][N - 1 - row] = temp; visited[row*N + col] = true; visited[(N - 1 - col)*N +row] = true; visited[(N - 1 - row)*N + N - 1 - col] = true; visited[(col)*N + N - 1 - row] = true; } } }