A matrix is symmetric, if it is equal to its transpose. This means all elements at index ( i , j )should be equal to the element at index ( j , i ). Given an input matrix mat. Check if the input matrix is symmetric.
| Input Parameters |
Expected outputs |
| mat = {
{1,4,5},
{4,1,3},
{5,3,1}};
|
The matrix is a Symmetric Matrix.
|
| mat= {
{0,1,1,0},
{1,1,1,1},
{0,1,1,0}}
|
The matrix is *not* a Symmetric Matrix.
|
| mat = {
{1,1},
{1,1},
{1,1},
{1,1}};
|
The matrix is *not* a Symmetric Matrix.
|
| mat= {
{1,2,3,4},
{2,2,3,4},
{3,3,3,2},
{4,4,2,4}}
|
The matrix is a Symmetric Matrix.
|
| The java method should accept following input parameters: mat (integer matrix).
|
| If mat does not have equal rows and columns, then mat is not a symmetric matrix, return false.
|
| Iterate through mat using a for loop, using variable rIdx as loop counter. Loop variable rIdx will be initialized with value 0 and will iterate through all the matrix’s columns one by one till rIdx < mat.length:
Iterate through mat using a for loop, using variable cIdx as loop counter. Loop variable cIdx will be initialized with value 0 and will iterate through all the matrix’s columns one by one till cIdx < mat[0].length:
If mat[rIdx][cIdx] != mat [cIdx][rIdx], return false else continue iterating.
|
| If we have completed above loops without finding any unequal elements, return true, as the matrix is symmetric.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class MdArraySymmetricMatrix {
public boolean matArraySymmetricMatrix(int[][] mat) throws Exception{
if (mat.length != mat[0].length) { return false; }
for (int rIdx=0; rIdx < mat.length; rIdx++) {
for (int cIdx=0; cIdx < mat[0].length; cIdx++) {
if (mat[rIdx][cIdx] != mat[cIdx][rIdx]) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
MdArraySymmetricMatrix ap=new MdArraySymmetricMatrix();
boolean ret;
try {
int[][] arr = { {1,4,5},
{4,1,3},
{5,3,1}
};
print2dArray(arr, "The 2d Array");
ret=ap.matArraySymmetricMatrix(arr);
if (ret) {
System.out.println("\n The matrix is an Symmteric Matrix");
}else {
System.out.println("\n The matrix is *not* an Symmteric Matrix");
}
int[][] arr1 = { {0,1,1,0},
{1,1,1,1},
{0,1,1,0},
};
print2dArray(arr1, "The 2d Array");
ret=ap.matArraySymmetricMatrix(arr1);
if (ret) {
System.out.println("\n The matrix is an Symmteric Matrix");
}else {
System.out.println("\n The matrix is *not* an Symmteric Matrix");
}
int[][] arr2 = { {1,1},
{1,1},
{1,1},
{1,1}
};
print2dArray(arr2, "The 2d Array");
ret=ap.matArraySymmetricMatrix(arr2);
if (ret) {
System.out.println("\n The matrix is an Symmteric Matrix");
}else {
System.out.println("\n The matrix is *not* an Symmteric Matrix");
}
int[][] arr3 = { {1,2,3,4},
{2,2,3,4},
{3,3,3,2},
{4,4,2,4}
};
print2dArray(arr3, "The 2d Array");
ret=ap.matArraySymmetricMatrix(arr3);
if (ret) {
System.out.println("\n The matrix is an Symmteric Matrix");
}else {
System.out.println("\n The matrix is *not* an Symmteric Matrix");
}
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void print2dArray(int[][] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input 2d Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("*********************************************************************");
System.out.print(label+" : \n");
int rIdx=0; int cIdx=0;
for (rIdx=0; rIdx < intArray.length; rIdx++) {
for (cIdx=0; cIdx < intArray[rIdx].length; cIdx++) {
System.out.print(intArray[rIdx][cIdx]);
if (cIdx < intArray[rIdx].length) System.out.print(",");
}
System.out.print("\n");
}
System.out.println();
System.out.println("**********************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}