
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line consists of two integer ‘n’ and ‘m’ denoting the size of rowSum array and colSum array.
The next line contains ‘n’ space-separated integers denoting the elements of ‘rowSum’.
The next line contains ‘m’ space-separated integers denoting the elements of ‘colSum’.
Return the valid matrix. There can be many possible solutions, return any of them.
If the returned matrix is valid matrix, the checker will return true else will return false
1 <= ’T’ <= 10
1 <= ‘n’<= 5000
1 <= arr[i][0] ,arrr[i][1] <= 10^6
Where ‘i’ varies from 1 to ‘n’ - 1 where ‘n’ is the length of the array ‘nums’.
Time Limit: 1 sec
You do not need to print anything, it has already been taken care of. Just implement the given function.
The Matrix[i][j] is equal to min(rowSum[i],colSum[j]) and then update rowSum[i] and colSum[ j ] such that rowSum[i]-=Matrix[i][j] and colSum[j]-=Matrix[i][j].In this way it is guaranteed that it will make a valid matrix and satisfy the above conditions.
Algorithm:-
In Approach1 instead of iterating all values matrix we can iterate only through rowSums and colSums values such that if rowSum[ i ] equal to zero then move to next element similarly in colSum and default values of ‘ans’ matrix will be 0. In this way, it is guaranteed that it will make a valid matrix and satisfy the above conditions.
Algorithm:-