forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet Matrix Zero.py
41 lines (33 loc) · 1.05 KB
/
Set Matrix Zero.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def setMatrixZeroes(matrix):
n = len(matrix)
m = len(matrix[0])
# To store which rows and columns are supposed to be marked with zeroes
row = [0] * n
col = [0] * m
# Traverse the matrix using nested loops
for i in range(n):
for j in range(m):
# If the cell contains zero, mark its row and column
if matrix[i][j] == 0:
row[i] = 1
col[j] = 1
# Update the matrix
for i in range(n):
for j in range(m):
# Set cells to zero if any of the row[i] or col[j] is marked
if row[i] or col[j]:
matrix[i][j] = 0
# Print the updated matrix
for row in matrix:
print(" ".join(map(str, row)))
# Driver Code
n = int(input("Enter number of rows: "))
m = int(input("Enter number of columns: "))
# Initialize matrix from user input
matrix = []
print("Enter the elements row-wise (space-separated):")
for i in range(n):
row = list(map(int, input().split()))
matrix.append(row)
# Function call
setMatrixZeroes(matrix)