| Input |
Expected outputs |
SumRows : 3,8
SumCols : 4,7
|
The Restored Matrix :
3,0,
1,7,
|
SumRows : 4,8
SumCols : 5,7
|
The Restored Matrix :
4,0,
1,7,
|
SumRows : 6,8,11
SumCols : 9,7,9
|
The Restored Matrix :
6,0,0,
3,5,0,
0,2,9,
|
| The java method should accept following input parameters: int array sumRows, int array sumCols.
|
| Declare new int matrix named nMatrix with sumRows.length rows and sumCols.length columns.
|
| Initialize variable min=0.
|
| Iterate through nMatrix rows 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 < nMatrix.length:
Iterate through nMatrix columns 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 < nMatrix[0].length:
Check if (sumRows[rIdx] < sumCols[cIdx]) in that case set min to sumRows[rIdx] else set min to sumCols[cIdx].
Set nMatrix[rIdx][cIdx]=min.
Decrement sumRows[rIdx] by min.
Decrements sumCols[cIdx] by min.
|
| When above for loops are completed, nMatrix contains elements restored based on row and column sums from from sumRows and sumCols. Return nMatrix.
|
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 MdArrayRestoreMatrixFromSums {
public int[][] restoreMatrixFromSums(int[]sumRows, int[] sumCols) throws Exception{
int[][] nMatrix=new int[sumRows.length][sumCols.length];
int min=0;
for (int rIdx=0; rIdx < sumRows.length; rIdx++) {
for (int cIdx=0; cIdx < sumCols.length; cIdx++) {
if (sumRows[rIdx] < sumCols[cIdx]) {
min=sumRows[rIdx];
}else {
min=sumCols[cIdx];
}
nMatrix[rIdx][cIdx]=min;
sumRows[rIdx] -=min;
sumCols[cIdx] -=min;
}
}
return nMatrix;
}
public static void main(String[] args) {
MdArrayRestoreMatrixFromSums ap=new MdArrayRestoreMatrixFromSums();
int[][] retArr;
try {
int[] sumRows = {3,8};
int[] sumCols = {4,7};
printArraySummary(sumRows, "SumRows");
printArraySummary(sumCols, "SumCols");
retArr=ap.restoreMatrixFromSums(sumRows, sumCols);
print2dArray(retArr, "The Restored Matrix");
int[] sumRows1 = {4,8};
int[] sumCols1 = {5,7};
printArraySummary(sumRows1, "SumRows");
printArraySummary(sumCols1, "SumCols");
retArr=ap.restoreMatrixFromSums(sumRows1, sumCols1);
print2dArray(retArr, "The Restored Matrix");
int[] sumRows2 = {6,8,11};
int[] sumCols2 = {9,7,9};
printArraySummary(sumRows2, "SumRows");
printArraySummary(sumCols2, "SumCols");
retArr=ap.restoreMatrixFromSums(sumRows2, sumCols2);
print2dArray(retArr, "The Restored 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;
}
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;
}
}