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

This problem involves reconstructing an original matrix when you know the largest element in each row and column, plus a binary matrix indicating where positive values were located. The key insight is that at any position where the binary matrix has a 1, the original value must be the minimum of the row maximum and column maximum.

Problem Understanding

Given:

  • Array A of size N (maximum elements for each row)

  • Array B of size M (maximum elements for each column)

  • Binary matrix of size N×M where 1 indicates a positive value existed, 0 indicates the position was 0

We need to reconstruct the original matrix where A[i] is the largest element in row i and B[j] is the largest element in column j.

Algorithm

The solution follows these steps ?

  • For each position (i, j) in the matrix:

    • If binary matrix[i][j] == 1, set original[i][j] = min(A[i], B[j])

    • If binary matrix[i][j] == 0, set original[i][j] = 0

Example

def print_original_mat(A, B, mat):
    N = len(A)
    M = len(B)
    
    print("Reconstructed matrix:")
    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()  # New line after each row

# Test data
A = [4, 2, 3]  # Row maximums
B = [3, 1, 0, 0, 4, 0, 5]  # Column maximums
binary_matrix = [
    [1, 0, 0, 0, 1, 0, 1],
    [0, 0, 1, 0, 0, 1, 1],
    [1, 1, 0, 1, 1, 0, 0]
]

print("Row maximums:", A)
print("Column maximums:", B)
print("Binary matrix:")
for row in binary_matrix:
    print(row)
print()

print_original_mat(A, B, binary_matrix)
Row maximums: [4, 2, 3]
Column maximums: [3, 1, 0, 0, 4, 0, 5]
Binary matrix:
[1, 0, 0, 0, 1, 0, 1]
[0, 0, 1, 0, 0, 1, 1]
[1, 1, 0, 1, 1, 0, 0]

Reconstructed matrix:
3 0 0 0 4 0 4 
0 0 0 0 0 0 2 
3 1 0 0 3 0 0 

How It Works

The algorithm works because:

  • At position (i,j), if there was a positive value, it cannot exceed either the row maximum A[i] or column maximum B[j]

  • To satisfy both constraints simultaneously, the value must be min(A[i], B[j])

  • This ensures that A[i] remains the maximum in row i and B[j] remains the maximum in column j

Verification Example

def verify_solution(original_matrix, A, B):
    """Verify that the reconstructed matrix satisfies the constraints"""
    N = len(original_matrix)
    M = len(original_matrix[0])
    
    # Check row maximums
    for i in range(N):
        row_max = max(original_matrix[i])
        if row_max != A[i]:
            return False, f"Row {i} max is {row_max}, expected {A[i]}"
    
    # Check column maximums
    for j in range(M):
        col_max = max(original_matrix[i][j] for i in range(N))
        if col_max != B[j]:
            return False, f"Column {j} max is {col_max}, expected {B[j]}"
    
    return True, "All constraints satisfied"

# Create the reconstructed matrix for verification
reconstructed = []
for i in range(len(A)):
    row = []
    for j in range(len(B)):
        if binary_matrix[i][j] == 1:
            row.append(min(A[i], B[j]))
        else:
            row.append(0)
    reconstructed.append(row)

is_valid, message = verify_solution(reconstructed, A, B)
print(f"Verification: {message}")
Verification: All constraints satisfied

Conclusion

The solution uses the principle that at any non-zero position, the value must be the minimum of the row and column maximums to satisfy both constraints. This approach efficiently reconstructs the original matrix while maintaining the given row and column maximum properties.

Updated on: 2026-03-25T09:54:28+05:30

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements