-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatZeros.java
More file actions
51 lines (42 loc) · 1.21 KB
/
MatZeros.java
File metadata and controls
51 lines (42 loc) · 1.21 KB
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
import java.util.HashSet;
import java.util.Scanner;
public class MatZeros {
public static void SetZeroes(int [][]mat){
int n = mat.length;
int m = mat[0].length;
HashSet<Integer> st1 = new HashSet<>();
HashSet<Integer> st2 = new HashSet<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < m ; j++){
if(mat[i][j] == 0){
st1.add(i);
st2.add(j);
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(st1.contains(i) || st2.contains(j)){
mat[i][j] = 0;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int [][]mat = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
mat[i][j] = sc.nextInt();
}
}
SetZeroes(mat);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
System.out.print(mat[i][j] + " ");
}
}
}
}