Lower triangle elements are the elements at indexes below the diagonal indexes of the matrix. Given input matrix mat, return the elements that are part of the lower triangle of the matrix.
| Input |
Expected outputs |
mat = { {1,2,3}, {4,5,6}, {7,8,9}};
|
Lower Triangle elements: 4,7,8.
|
mat = { {55,66,77,88}, {11,22,33,44}, {99,109,119,129}, {44,66,11,22}};
|
Lower Triangle elements: 11,99,109,44,66,11.
|
mat = { {30,40}, {50,60}, };
|
Lower Triangle element: 50.
|
| The java method should accept following input parameters: mat (integer matrix).
|
| Initialize an int variable n and set it to count of rows of mat.
|
| Initialize an int array named triangleElements with length (n*(n-1))/2.
|
| Initialize an int variable tIdx=0 to hold the index value of triangleElements array.
|
| 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 (rIdx > cIdx), then set triangleElements[tIdx]=mat[rIdx][cIdx].
Increment tIdx by 1.
|
| At the end of above loop, return the triangleElements array with the lower triangle elements.
|
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 MdArrayLowerTriangle {
public int[] matLowerTriangle(int[][] mat) throws Exception{
int n=mat.length; int tIdx=0;
int[] triangleElements=new int[(n*(n-1))/2];
for (int rIdx=0; rIdx < mat.length; rIdx++) {
for (int cIdx=0; cIdx < mat[0].length; cIdx++) {
if (rIdx > cIdx) {
triangleElements[tIdx]=mat[rIdx][cIdx];
tIdx++;
}
}
}
return triangleElements;
}
public static void main(String[] args) {
MdArrayLowerTriangle ap=new MdArrayLowerTriangle();
int ret[];
try {
int[][] arr = { {1,2,3},
{4,5,6},
{7,8,9}
};
print2dArray(arr, "The 2d Array");
ret=ap.matLowerTriangle(arr);
printArraySummary(ret, "Lower Triangle");
int[][] arr1 = { {55,66,77,88},
{11,22,33,44},
{99,109,119,129},
{44,66,11,22}};
print2dArray(arr1, "The 2d Array");
ret=ap.matLowerTriangle(arr1);
printArraySummary(ret, "Lower Triangle");
int[][] arr2 = { {30,40},
{50,60},
};
print2dArray(arr2, "The 2d Array");
ret=ap.matLowerTriangle(arr2);
printArraySummary(ret, "Lower Triangle");
}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;
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input 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+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex < intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}