-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion50.java
More file actions
47 lines (43 loc) · 1.31 KB
/
question50.java
File metadata and controls
47 lines (43 loc) · 1.31 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
public class question50 {
public static void modifyMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean[] rowFlag = new boolean[rows];
boolean[] colFlag = new boolean[cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 1) {
rowFlag[i] = true;
colFlag[j] = true;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (rowFlag[i] || colFlag[j]) {
matrix[i][j] = 1;
}
}
}
}
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] matrix = {
{1, 0, 0},
{0, 0, 0},
{0, 0, 1}
};
System.out.println("Original Matrix:");
printMatrix(matrix);
modifyMatrix(matrix);
System.out.println("Modified Matrix:");
printMatrix(matrix);
}
}