Last active
April 6, 2020 06:09
-
-
Save dsapandora/d2011a7464702ec95bdce2fdfe5f2cb5 to your computer and use it in GitHub Desktop.
rotate 90 degrees a matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C++ program to rotate a matrix by 90 degrees | |
#include <bits/stdc++.h> | |
#define N 4 | |
using namespace std; | |
void displayMatrix(int mat[N][N]); | |
// An Inplace function to rotate a N x N matrix | |
// by 90 degrees in anti-clockwise direction | |
void rotateMatrix(int mat[][N]) | |
{ | |
// Consider all squares one by one | |
for (int x = 0; x < N / 2; x++) { | |
// Consider elements in group of 4 in | |
// current square | |
for (int y = x; y < N - x - 1; y++) { | |
// store current cell in temp variable | |
int temp = mat[x][y]; | |
// move values from right to top | |
mat[x][y] = mat[y][N - 1 - x]; | |
// move values from bottom to right | |
mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y]; | |
// move values from left to bottom | |
mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x]; | |
// assign temp to left | |
mat[N - 1 - y][x] = temp; | |
} | |
} | |
} | |
// Function to print the matrix | |
void displayMatrix(int mat[N][N]) | |
{ | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) | |
printf("%2d ", mat[i][j]); | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
/* Driver program to test above functions */ | |
int main() | |
{ | |
// Test Case 1 | |
int mat[N][N] = { | |
{ 1, 2, 3, 4 }, | |
{ 5, 6, 7, 8 }, | |
{ 9, 10, 11, 12 }, | |
{ 13, 14, 15, 16 } | |
}; | |
// Tese Case 2 | |
/* int mat[N][N] = { | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9} | |
}; | |
*/ | |
// Tese Case 3 | |
/*int mat[N][N] = { | |
{1, 2}, | |
{4, 5} | |
};*/ | |
// displayMatrix(mat); | |
rotateMatrix(mat); | |
// Print rotated matrix | |
displayMatrix(mat); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python3 program to rotate a matrix by 90 degrees | |
N = 4 | |
# An Inplace function to rotate | |
# N x N matrix by 90 degrees in | |
# anti-clockwise direction | |
def rotateMatrix(mat): | |
# Consider all squares one by one | |
for x in range(0, int(N / 2)): | |
# Consider elements in group | |
# of 4 in current square | |
for y in range(x, N-x-1): | |
# store current cell in temp variable | |
temp = mat[x][y] | |
# move values from right to top | |
mat[x][y] = mat[y][N-1-x] | |
# move values from bottom to right | |
mat[y][N-1-x] = mat[N-1-x][N-1-y] | |
# move values from left to bottom | |
mat[N-1-x][N-1-y] = mat[N-1-y][x] | |
# assign temp to left | |
mat[N-1-y][x] = temp | |
# Function to print the matrix | |
def displayMatrix( mat ): | |
for i in range(0, N): | |
for j in range(0, N): | |
print (mat[i][j], end = ' ') | |
print ("") | |
# Driver Code | |
mat = [[0 for x in range(N)] for y in range(N)] | |
# Test case 1 | |
mat = [ [1, 2, 3, 4 ], | |
[5, 6, 7, 8 ], | |
[9, 10, 11, 12 ], | |
[13, 14, 15, 16 ] ] | |
''' | |
# Test case 2 | |
mat = [ [1, 2, 3 ], | |
[4, 5, 6 ], | |
[7, 8, 9 ] ] | |
# Test case 3 | |
mat = [ [1, 2 ], | |
[4, 5 ] ] | |
''' | |
rotateMatrix(mat) | |
# Print rotated matrix | |
displayMatrix(mat) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Complexity Analysis:
Time Complexity : O(n*n), where n is side of array, A single traversal of the matrix is needed.
Space Complexity : O(1), constant space is needed