-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolean Matrix.java
More file actions
80 lines (67 loc) · 2.18 KB
/
Boolean Matrix.java
File metadata and controls
80 lines (67 loc) · 2.18 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split("\\s+");
int r = Integer.parseInt(str[0]);
int c = Integer.parseInt(str[1]);
int matrix[][] = new int[r][c];
for(int i = 0; i < r; i++)
{
int k = 0;
str = read.readLine().trim().split("\\s+");
for(int j = 0; j < c; j++){
matrix[i][j] = Integer.parseInt(str[k]);
k++;
}
}
new Solution().booleanMatrix(matrix);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
sb.append(matrix[i][j] + " ");
}
sb.append("\n");
}
System.out.print(sb);
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
//Function to modify the matrix such that if a matrix cell matrix[i][j]
//is 1 then all the cells in its ith row and jth column will become 1.
void booleanMatrix(int matrix[][])
{
// code here
int n = matrix.length;
int m = matrix[0].length;
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(matrix[i][j] == 1){
row[i] = 1;
col[j] = 1;
}
//let's fill the rows
for(int i = 0 ; i < n ; i ++)
if(row[i] == 1)
for(int j = 0 ; j < m ; j++)
matrix[i][j] = 1;
for(int j = 0 ; j < m ; j++)
if(col[j] == 1)
for(int i = 0 ; i < n ; i ++)
matrix[i][j] = 1;
}
}