
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Program to find valid matrix given row and column sums in Python
- Find Largest & Smallest Element in Each Column of a Matrix in Java?
- Find the maximum element of each row in a matrix using Python
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- Find the column number with largest value for each row in an R matrix.
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Program to find smallest intersecting element of each row in a matrix in Python
- Find maximum element of each row in a matrix in C++
- Find Maximum Element in Each Row of a Matrix in Java
- How to find the column names and row names from a matrix in R?
- Find Largest & Smallest Element in Primary and Secondary Diagonal of a Matrix in Java
- Program to find area of largest square of 1s in a given matrix in python
- Python Program to find the largest element in a tuple
- Python Program To Find The Largest Element In A Dictionary
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
