Write an algorithm such that if an element in an MxN matrix is 0, its entire row
and column are set to 0
package test;
public class JumpTwo {
public static void main(String[] args) {
int[][] matr = {{1,2,0},{4,5,6},{7,8,9},{10,0,1}};
int n=4, m=3;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print("\t"+ matr[i][j]);
}
System.out.println();
}
System.out.println("after setting zeros");
set0(matr,n,m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print("\t"+ matr[i][j]);
}
System.out.println();
}
}
public static void set0(int[][] s, int n, int m) {
int [] row = new int[n];
int [] col = new int[m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(s[i][j]==0) {
row[i]=1;
col[j]=1;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(row[i]+col[j]>=1) s[i][j]=0;
}
}
}
}