Suppose we have two arrays A and B of size N and M respectively and we also have one N X M binary matrix where 1 denotes that there was a positive integer in the original matrix and 0 means that position is holding 0 into the original matrix also. We have to generate the original matrix so that A[i] denotes the largest element in the ith row and B[j] denotes the largest element in the jth column.
So, if the input is like A = [4, 2, 3], B = [3, 1, 0, 0, 4, 0, 5] matrix, then the output will be matrix
To solve this, we will follow these steps −
N := size of A
M := size of B
for i in range 0 to N, do
for j in range 0 to M, do
if mat[i, j] is same as 1, then
display minimum of A[i] and B[j]
otherwise,
print a new line
Example
Let us see the following implementation to get better understanding −
def print_original_mat(A, B, mat) : N = len(A) M = len(B) for i in range(N) : for j in range(M) : if (mat[i][j] == 1) : print(min(A[i], B[j]), end = " ") else : print(0, end = " ") print() A = [4, 2, 3] B = [3, 1, 0, 0, 4, 0, 5] mat = [ [1, 0, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 1], [1, 1, 0, 1, 1, 0, 0]] print_original_mat(A, B, mat);
Input
[4, 2, 3], [3, 1, 0, 0, 4, 0, 5], [[1, 0, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 1], [1, 1, 0, 1, 1, 0, 0]]
Output
3 0 0 0 4 0 4 0 0 0 0 0 0 2 3 1 0 0 3 0 0