forked from ravikartar/hacktober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetmatrixzero
36 lines (33 loc) · 835 Bytes
/
setmatrixzero
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
#include<bits/stdc++.h>
using namespace std;
void setZeroes(vector < vector < int >> & matrix) {
int rows = matrix.size(), cols = matrix[0].size();
vector < int > dummy1(rows,-1), dummy2(cols,-1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
dummy1[i] = 0;
dummy2[j] = 0;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dummy1[i] == 0 || dummy2[j]==0) {
matrix[i][j] = 0;
}
}
}
}
int main() {
vector < vector < int >> arr;
arr = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
setZeroes(arr);
cout<<"The Final Matrix is "<<endl;
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < arr[0].size(); j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}
}