-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanMatrix-GFGQ.cpp
More file actions
137 lines (111 loc) · 2.76 KB
/
BooleanMatrix-GFGQ.cpp
File metadata and controls
137 lines (111 loc) · 2.76 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Boolean Matrix
Given a boolean matrix of size RxC where each cell contains either 0 or 1, modify it 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.
Example 1:
Input:
R = 2, C = 2
matrix[][] = {{1, 0},
{0, 0}}
Output:
1 1
1 0
Explanation:
Only cell that has 1 is at (0,0) so all
cells in row 0 are modified to 1 and all
cells in column 0 are modified to 1.
Example 2:
Input:
R = 4, C = 3
matrix[][] = {{ 1, 0, 0},
{ 1, 0, 0},
{ 1, 0, 0},
{ 0, 0, 0}}
Output:
1 1 1
1 1 1
1 1 1
1 0 0
Explanation:
The position of cells that have 1 in
the original matrix are (0,0), (1,0)
and (2,0). Therefore, all cells in row
0,1,2 are and column 0 are modified to 1.
Your Task:
You dont need to read input or print anything. Complete the function booleanMatrix() that takes the matrix as input parameter and modifies it in-place.
Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R + C)
Constraints:
1 ≤ R, C ≤ 1000
0 ≤ matrix[i][j] ≤ 1
*/
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//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(vector<vector<int> > &matrix)
{
// code here
int r=matrix.size();
int c=matrix[0].size();
vector<int>row(r,0);
vector<int>col(c,0);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(matrix[i][j]==1){
row[i]=1;
col[j]=1;
}
}
}
for(int i=0;i<r;i++){
if(row[i]==1){
for(int j=0;j<c;j++){
matrix[i][j]=1;
}
}
}
for(int j=0;j<c;j++){
if(col[j]==1){
for(int i=0;i<r;i++){
matrix[i][j]=1;
}
}
}
}
};
//{ Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--)
{
int row, col;
cin>> row>> col;
vector<vector<int> > matrix(row);
for(int i=0; i<row; i++)
{
matrix[i].assign(col, 0);
for( int j=0; j<col; j++)
{
cin>>matrix[i][j];
}
}
Solution ob;
ob.booleanMatrix(matrix);
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
// } Driver Code Ends