-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetmatrux.java
59 lines (51 loc) · 1.6 KB
/
Setmatrux.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
54
55
56
57
58
59
import java.util.*;
class MatrixCom {
public static void setZeroes(int[][] matrix, int n, int m) {
// Create sets to store the rows and columns to be zeroed
Set<Integer> zeroRows = new HashSet<>();
Set<Integer> zeroCols = new HashSet<>();
// Find the rows and columns containing zeros
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
zeroRows.add(i);
zeroCols.add(j);
}
}
}
// Set zeros for rows
for (int row : zeroRows) {
for (int j = 0; j < m; j++) {
matrix[row][j] = 0;
}
}
// Set zeros for columns
for (int col : zeroCols) {
for (int i = 0; i < n; i++) {
matrix[i][col] = 0;
}
}
}
public static void main(String[] args) {
int count = 0;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] matrix = new int[n][m];
// Input matrix elements
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
}
}
// Call the setZeroes method to modify the matrix
setZeroes(matrix, n, m);
// Print the modified matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}