Find Valid Matrix Given Row and Column Sums

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the i<sup>th</sup> row and colSum[j] is the sum of the elements of the j<sup>th</sup> column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.
Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies the rowSum and colSum requirements.
Return a 2D array representing any matrix that fulfills the requirements. It's guaranteed that at least one matrix that fulfills the requirements exists.
LeetCode Problem - 1605
class Solution {
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
// Get the number of rows (m) and columns (n) from the lengths of rowSum and colSum arrays
int m = rowSum.length;
int n = colSum.length;
// Initialize the result matrix with dimensions m x n
int[][] result = new int[m][n];
int i = 0; // row index
int j = 0; // column index
// Loop through the matrix until we reach the end of either the rows or columns
while (i < m && j < n) {
// Set the current cell to the minimum of the remaining row sum or column sum
result[i][j] = Math.min(rowSum[i], colSum[j]);
// Subtract the value placed in the current cell from the respective row sum and column sum
rowSum[i] -= result[i][j];
colSum[j] -= result[i][j];
// If the remaining row sum is zero, move to the next row
if (rowSum[i] == 0) i++;
// If the remaining column sum is zero, move to the next column
if (colSum[j] == 0) j++;
}
// Return the restored matrix
return result;
}
}




