Current code works but I want the code in the comments to work where user enters rows, columns and contents of array the question is to write an algorithm that if an element in an m*n matrix is 0, its entire row and column is set to 0
Expert Answer
Algorithm with example:
Suppose,
arr = [[1, 0, 1, 1, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1]]
Nob = len(arr)
### pass 1
# 1 rst line/column
tt = 1
for uu in range(Nob):
tt &= arr[uu][0]
l = 1
for uu in range(1,Nob):
l &= arr[0][uu]
# other line/cols
# use line1, col1 to keep only those with 1
for uu in range(1,Nob):
for qq in range(1,Nob):
if arr[uu][qq] == 0:
arr[0][qq] = 0
arr[uu][0] = 0
else:
arr[uu][qq] = 0
### pass 2
# if line1 and col1 are ones: it is 1
for uu in range(1,Nob):
for qq in range(1,Nob):
if arr[uu][0] & arr[0][qq]:
arr[uu][qq] = 1
# 1rst row and col: reset if 0
if l == 0:
for uu in range(Nob):
arr [uu][0] = 0
if tt == 0:
for qq in range(1,Nob):
arr [0][qq] = 0
print(arr)