From db95f459e8c7d151db6fd844f62c1ff1747e709d Mon Sep 17 00:00:00 2001 From: Motasim <44056349+motasimmakki@users.noreply.github.com> Date: Tue, 15 Jun 2021 18:41:44 +0530 Subject: [PATCH 1/4] feat: Rotate matrix 90` without extra space To rotate a given square matrix anti-clockwise in just O(1) extra space. --- Code/C++/RotateMatrixBy90.cpp | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Code/C++/RotateMatrixBy90.cpp diff --git a/Code/C++/RotateMatrixBy90.cpp b/Code/C++/RotateMatrixBy90.cpp new file mode 100644 index 000000000..7a53a1cb5 --- /dev/null +++ b/Code/C++/RotateMatrixBy90.cpp @@ -0,0 +1,70 @@ +#include // for `i/o operation` +#define N 3 // defining size `N` + +/* +function to swap values of two valiables +*/ +void swap(int &num1, int &num2){ + int temp = num1; + num1 = num2; + num2 = temp; +} + +/* +function to print the matrix in matrix form +*/ +void printMatrix(int matrix[N][N]){ + for(int i = 0; i < N; i++){ + for(int j = 0; j < N; j++){ + std::cout<< "["<< matrix[i][j]<< "]\t"; + } + std::cout<< std::endl; + } +} + +/* +main function or the driver function +*/ +int main(){ + /* ###Test Case: + Matrix: + 1 2 3 + 4 5 6 + 7 8 9 + After Rotation: + 3 6 9 + 2 5 6 + 7 8 9 + */ + + // Initializing matrix + int matrix[N][N] = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + std::cout<< "\nInitial Matrix Is: "<< std::endl; + printMatrix(matrix); + + for(int i = 0; i < N; i++){ + for(int j = i; j < N; j++){ + swap(matrix[i][j], matrix[j][i]); + } + } + // std::cout<< "\nTranspose Of Matrix Is: "<< std::endl; + // printMatrix(matrix); + + for(int i = 0; i < N; i++){ + int start = 0, end = N-1; + + while(start < end){ + swap(matrix[start][i], matrix[end][i]); + start += 1; + end -= 1; + } + } + std::cout<< "\n90` Rotation Of Matrix Is: "<< std::endl; + printMatrix(matrix); + + return 0; +} \ No newline at end of file From 229d0b7a65a1f8a5c88ba68d2002eb78f3029e8e Mon Sep 17 00:00:00 2001 From: Motasim <44056349+motasimmakki@users.noreply.github.com> Date: Tue, 15 Jun 2021 18:46:56 +0530 Subject: [PATCH 2/4] docs: Added anti-clockwise matrix rotation --- 2D_Array/readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/2D_Array/readme.md b/2D_Array/readme.md index 74f031c3a..350ada57f 100644 --- a/2D_Array/readme.md +++ b/2D_Array/readme.md @@ -22,3 +22,4 @@ Due to the fact that the elements of 2D arrays can be random accessed. Similar t * Substraction of two Matrices ----> [Java](/Code/Java/matrixop_sub.java) * Transpose of a Matrix --->[Java](/Code/Java/transpose.java) | [Python](/Code/Python/Transpose_of_matrix.py) * Wave Print ----> [C++](/Code/C++/wave_print.cpp) +* Rotate Matrix 90' Anti-Clockwise without extra space ----> [C++](/Code/C++/RotateMatrixBy90.cpp) From c3522b0610a7c959397fc36f1ce284030134a797 Mon Sep 17 00:00:00 2001 From: Motasim <44056349+motasimmakki@users.noreply.github.com> Date: Tue, 15 Jun 2021 23:18:51 +0530 Subject: [PATCH 3/4] style: Maintained alphabetical order --- 2D_Array/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2D_Array/readme.md b/2D_Array/readme.md index 350ada57f..794be96dd 100644 --- a/2D_Array/readme.md +++ b/2D_Array/readme.md @@ -17,9 +17,9 @@ Due to the fact that the elements of 2D arrays can be random accessed. Similar t * Multiplication of two Matrices ----> [Java](/Code/Java/matrixop_mul.java) * Overall Median of Matrix ---> [Java](/Code/Java/overall_median_matrix.java) * Row-wise sum ----> [C++](/Code/C++/row_wise_sum.cpp) | [java](/Code/java/RowWise_Sum.java) +* Rotate Matrix 90' Anti-Clockwise without extra space ----> [C++](/Code/C++/RotateMatrixBy90.cpp) * Spiral Print ----> [C++](/Code/C++/spiral_print.cpp) * Staircase Search ----> [C++](/Code/C++/staircase_search.cpp) * Substraction of two Matrices ----> [Java](/Code/Java/matrixop_sub.java) * Transpose of a Matrix --->[Java](/Code/Java/transpose.java) | [Python](/Code/Python/Transpose_of_matrix.py) * Wave Print ----> [C++](/Code/C++/wave_print.cpp) -* Rotate Matrix 90' Anti-Clockwise without extra space ----> [C++](/Code/C++/RotateMatrixBy90.cpp) From 79e2e6810a85c53fda2aca0c114b5fa47627c7c5 Mon Sep 17 00:00:00 2001 From: Motasim <44056349+motasimmakki@users.noreply.github.com> Date: Tue, 15 Jun 2021 23:30:36 +0530 Subject: [PATCH 4/4] docs: Included test case, time & space complexity --- Code/C++/RotateMatrixBy90.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Code/C++/RotateMatrixBy90.cpp b/Code/C++/RotateMatrixBy90.cpp index 7a53a1cb5..2fe84682c 100644 --- a/Code/C++/RotateMatrixBy90.cpp +++ b/Code/C++/RotateMatrixBy90.cpp @@ -26,7 +26,7 @@ void printMatrix(int matrix[N][N]){ main function or the driver function */ int main(){ - /* ###Test Case: + /* ###Test Case: 01 Matrix: 1 2 3 4 5 6 @@ -36,6 +36,16 @@ int main(){ 2 5 6 7 8 9 */ + /* ###Test Case: 02 + Matrix: + 10 9 4 + 3 4 20 + 50 12 19 + After Rotation: + 4 20 19 + 9 4 12 + 10 3 50 + */ // Initializing matrix int matrix[N][N] = { @@ -67,4 +77,7 @@ int main(){ printMatrix(matrix); return 0; -} \ No newline at end of file +} + +// Time Complexity: O(N*N), Where 'N' is the size of square matrix +// Auxiliary Space: O(1), No extra space has been used