Ref : complete solution if 1st row doesn't have any 0 then how do we get back its

Ref : complete solution
if 1st row doesn’t have any 0 then how do we get back its original values, as it is storing information of other 0s in the matrix ?

So, you have 2 additional variables ( lets say R and C ). Firstly convince yourself that if in the original array, if any of the value in the first row was 0, the whole row will be 0. So R stores just that information ( whether any of the value in the row was 0 ). Now, if R = 0, your job is simple. In the end, mark every element in the first row as 0. If R = 1, then leave the row as it is ( with each cell storing whether their corresponding column has any zeroes ).
Things work very similarily for columns.

Let me demonstrate using an example.
Lets say the array was
[
1 1 1
0 1 1
1 0 1
]

Now, R = 1 as everything in row 1 = 1. C = 0, as second element in first column is 0.
Now, using first row and first column to store value about their respective columns and rows ( For the first row, column i stores if all the value in column i are 1. For the first column, row i stores if all the values in row i are 1. We leave 0,0 unassigned )

[
X 0 1
0 . .
0 . .
]
is what you get.

Now, for any entry which is not in first row or first column, entry (r,c) is 1, if its corresponding column entry in first row is 1 and corresponding row entry in first column is 1.

[
0 0 1
0 0 0
0 0 0
]

Let me know if that makes sense.

Click here to start solving coding interview questions