Check Rotated Matrix Equality
This program checks if one matrix is rotation of another.
Problem Statement
Write a Java program to check whether matrix B is rotation of matrix A by 90 degrees.
Source Code
| 1 | public class MatrixRotationCheck { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Matrix Rotation Check"); |
| 4 | int[][] A = {{1,2,3},{4,5,6},{7,8,9}}; |
| 5 | int[][] B = {{7,4,1},{8,5,2},{9,6,3}}; |
| 6 | boolean rotated=true; |
| 7 | for(int i=0;i<3;i++){ |
| 8 | for(int j=0;j<3;j++){ |
| 9 | if(B[i][j]!=A[3-1-j][i]) rotated=false; |
| 10 | } |
| 11 | } |
| 12 | if(rotated) System.out.println("Matrix B is rotation of A"); |
| 13 | else System.out.println("Matrix B is not rotation of A"); |
| 14 | } |
| 15 | } |
Program Output
Eduinq Matrix Rotation Check Matrix B is rotation of A
Explanation
The rotation check compares elements of B with the corresponding rotated positions of A using the formula B[i][j] == A[n-1-j][i], and nested loops validate this condition across the whole matrix, demonstrating a rotated matrix equality check.