forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeromatrix.java
53 lines (50 loc) · 847 Bytes
/
zeromatrix.java
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
42
43
44
45
46
47
48
49
50
51
52
53
package development;
import java.util.*;
public class zeromatrix {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int []row = new int[a];
int []col = new int[b];
int[][]arr = new int[a][b];
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
arr[i][j]= scan.nextInt();
if(arr[i][j]==0)
{
row[i]++;
col[j]++;
}
}
}
for(int i=0;i<a;i++)
{
if(row[i]>0)
{
for(int j=0;j<b;j++) {
arr[i][j]=0;
}
}
}
for(int i=0;i<b;i++)
{
if(col[i]>0)
{
for(int j=0;j<a;j++) {
arr[j][i]=0;
}
}
}
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}