forked from jaspreetkaur96/CodingQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoolean Matrix.java
79 lines (66 loc) · 1.94 KB
/
Boolean Matrix.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tests = Integer.parseInt(br.readLine());
for(int t = 0 ; t < tests ; t++)
{
String order [] = br.readLine().split(" ");
int rows = Integer.parseInt(order[0]);
int cols = Integer.parseInt(order[1]);
boolean row_array[] = new boolean[rows];
boolean col_array[] = new boolean[cols];
boolean a [][] = new boolean[rows][cols];
for(int i = 0 ; i < rows ; i++)
{
String split_row [] = br.readLine().split(" ");
for(int j = 0 ; j < cols ; j++)
{
a[i][j] = Integer.parseInt(split_row[j]) > 0 ? true : false;
// a[i][j] = i > 0 ? true : falsesc.nextBoolean();
if(a[i][j] == true)
{
row_array[i] = true;
col_array[j] = true;
}
}
}
for(int j = 0 ; j < cols ;j++) //row 0
{
if(col_array[j] == true)
{
for(int i = 0 ; i < rows; i++)
{
a[i][j] = true;
}
}
}
for(int j = 0 ; j < rows ;j++) //col 1
{
if(row_array[j] == true)
{
for(int i = 0 ; i < cols; i++)
{
a[j][i] = true;
}
}
}
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < rows ; i++)
{
for(int j = 0 ; j < cols ; j++)
{
if(a[i][j] == true)
sb.append(1 +" ");
else sb.append(0 +" ");
}
sb.append("\n");
}
sb.setLength(sb.length() - 1);
System.out.println(sb);
}
}
}