-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet_Matrix_Zero.C++
46 lines (42 loc) · 923 Bytes
/
Set_Matrix_Zero.C++
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
#include<bits/stdc++.h>
using namespace std;
//Set Matrix Zero
void set_zero(int i,int j,int n,int m,vector<vector<int>>& arr){
//rows
for(int k=0;k<m;k++){
arr[i][k] = 0;
}
//cols
for(int l=0;l<n;l++){
arr[l][j] = 0;
}
}
void get_zero(int n,int m, vector<vector<int>>& arr){
vector<pair<int,int>>v;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j] == 0)
v.push_back({i,j});
}
}
for(int k=0;k<v.size();k++){
set_zero(v[k].first,v[k].second,n,m,arr);
}
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> arr(n,vector<int>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}
get_zero(n,m,arr);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}