Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added program to rotate any matrix NxN #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Programs/matrix/RotateMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Programs.matrix;


import java.util.Arrays;

public class RotateMatrix {
static boolean rotate(int[][] matrix){
if(matrix.length == 0 || matrix.length != matrix[0].length) return false;
int n = matrix.length;

for(int layer =0; layer < n/2;layer++){
int first = layer;
int last = n-1-layer;
for(int i = first; i<last;i++){
int offset = i - first;
int top = matrix[first][i];

matrix[first][i] = matrix[last -offset][first];
matrix[last - offset][first] = matrix[last][last - offset];
matrix[last][last -offset] = matrix[i][last];
matrix[i][last] = top;
}
}
System.out.println(Arrays.deepToString(matrix));
return true;
}

public static void main(String[] args) {
int[][] matrix = {
{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18},
{19,20,21,22,23,24},
{25,26,27,28,29,30},
{31,32,33,34,35,36}};
System.out.println(rotate(matrix));
}
}
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ It is very easy to contribute, you may follow these steps -
95. [Strassen's Multiplication](https://github.com/PrajaktaSathe/Java/blob/main/Programs/StrassensMultiplication.java) - Program to multiply two square matrices using Strassen's Multiplication method.
96. [Xor Palindrome](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Xorpalindrome.java) - Program/Solution to Xor Palindrome
97. [Streams API](./Programs/JavaStreams.java) - Program to demo the Java Stream API with String and Integer Lists.
98.[Stack](https://github.com/PrajaktaSathe/Java/blob/main/Stack.java)-Program to demo the stack implementation
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
98. [Stack](https://github.com/PrajaktaSathe/Java/blob/main/Stack.java)-Program to demo the stack implementation
99. [RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
101. [Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
102. [Rotate Matrix](./Programs/matrix/RotateMatrix.java) - Program to rotate a matrix of NxN in 90 degrees.


# Contributors -
## A big thank you to all our contributors!!!
Expand Down