Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to rotate a matrix of size n*n to 90-degree k times using C#?
Rotating a matrix by 90 degrees is a common operation in programming. When we need to rotate a matrix k times, we can optimize by noting that 4 rotations return the matrix to its original state. Therefore, we only need to perform k % 4 rotations.
The algorithm works by processing concentric squares within the matrix. For an n×n matrix, there are n/2 concentric squares. In each square, elements move in a cycle of 4 positions during a 90-degree clockwise rotation.
How the Rotation Algorithm Works
During a 90-degree clockwise rotation, each element moves according to this pattern −
Element at position (i,j) moves to position (j, n-1-i)
Element at position (j, n-1-i) moves to position (n-1-i, n-1-j)
Element at position (n-1-i, n-1-j) moves to position (n-1-j, i)
Element at position (n-1-j, i) moves to position (i,j)
We process each concentric square layer by layer, swapping elements in groups of 4 to complete the rotation.
Example
using System;
using System.Text;
namespace ConsoleApplication {
public class Matrix {
public void RotateMatrixByKTimes(int[,] matrix, int numberOfTimes) {
int n = matrix.GetLength(0);
// Optimize: only need k % 4 rotations since 4 rotations = original
numberOfTimes = numberOfTimes % 4;
for (int k = 0; k < numberOfTimes; k++) {
// Process each concentric square
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - i - 1; j++) {
// Store top element
int temp = matrix[i, j];
// Move left to top
matrix[i, j] = matrix[n - 1 - j, i];
// Move bottom to left
matrix[n - 1 - j, i] = matrix[n - i - 1, n - 1 - j];
// Move right to bottom
matrix[n - i - 1, n - 1 - j] = matrix[j, n - i - 1];
// Move top to right
matrix[j, n - i - 1] = temp;
}
}
}
// Display the rotated matrix
PrintMatrix(matrix);
}
public void PrintMatrix(int[,] matrix) {
int n = matrix.GetLength(0);
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.Append(matrix[i, j] + " ");
}
Console.WriteLine(sb.ToString().Trim());
}
}
}
class Program {
static void Main(string[] args) {
Matrix m = new Matrix();
int[,] matrix = {
{ 5, 1, 9, 11 },
{ 2, 4, 8, 10 },
{ 13, 3, 6, 7 },
{ 15, 14, 12, 16 }
};
Console.WriteLine("Original Matrix:");
m.PrintMatrix((int[,])matrix.Clone());
Console.WriteLine("\nAfter 2 rotations (180 degrees):");
m.RotateMatrixByKTimes(matrix, 2);
}
}
}
The output of the above code is −
Original Matrix: 5 1 9 11 2 4 8 10 13 3 6 7 15 14 12 16 After 2 rotations (180 degrees): 16 12 14 15 7 6 3 13 10 8 4 2 11 9 1 5
Optimized Approach for Multiple Rotations
Since rotating a matrix 4 times returns it to the original state, we can optimize by calculating k % 4 −
using System;
class OptimizedRotation {
static void Main() {
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
Console.WriteLine("Testing different rotation counts:");
// Test k = 5 (equivalent to k = 1)
int[,] copy = (int[,])matrix.Clone();
RotateMatrix(copy, 5);
Console.WriteLine("<br>5 rotations (same as 1 rotation):");
PrintMatrix(copy);
// Test k = 8 (equivalent to k = 0)
copy = (int[,])matrix.Clone();
RotateMatrix(copy, 8);
Console.WriteLine("<br>8 rotations (same as 0 rotations - original):");
PrintMatrix(copy);
}
static void RotateMatrix(int[,] matrix, int k) {
int n = matrix.GetLength(0);
k = k % 4; // Optimization: only need k % 4 rotations
for (int rotation = 0; rotation < k; rotation++) {
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - i - 1; j++) {
int temp = matrix[i, j];
matrix[i, j] = matrix[n - 1 - j, i];
matrix[n - 1 - j, i] = matrix[n - 1 - i, n - 1 - j];
matrix[n - 1 - i, n - 1 - j] = matrix[j, n - 1 - i];
matrix[j, n - 1 - i] = temp;
}
}
}
}
static void PrintMatrix(int[,] matrix) {
int n = matrix.GetLength(0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Testing different rotation counts: 5 rotations (same as 1 rotation): 7 4 1 8 5 2 9 6 3 8 rotations (same as 0 rotations - original): 1 2 3 4 5 6 7 8 9
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time Complexity | O(n²) | We visit each element once per rotation |
| Space Complexity | O(1) | In-place rotation using only temporary variables |
