- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the original matrix when largest element in a row and a column are given in Python

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

- Related Questions & Answers
- Program to find valid matrix given row and column sums in Python
- Find the column number with largest value for each row in an R matrix.
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- Find a common element in all rows of a given row-wise sorted matrix in C++
- How to find the column names and row names from a matrix in R?
- Find maximum element of each row in a matrix in C++
- Program to find smallest intersecting element of each row in a matrix in Python
- Program to find area of largest square of 1s in a given matrix in python
- Find smallest and largest element from square matrix diagonals in C++
- Find maximum element of each column in a matrix in C++
- Write a program in Python to find the lowest value in a given DataFrame and store the lowest value in a new row and column
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- C++ program to find the Sum of each Row and each Column of a Matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?