#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> zeroMatrix(vector<vector<int>> &matrix, int n, int m) {
int row[n] = {0}; // row array
int col[m] = {0}; // col array
// Traverse the matrix:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
// mark ith index of row wih 1:
row[i] = 1;
// mark jth index of col wih 1:
col[j] = 1;
}
}
}
// Finally, mark all (i, j) as 0
// if row[i] or col[j] is marked with 1.
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
return matrix;
}
int main()
{
vector<vector<int>> matrix = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
int n = matrix.size();
int m = matrix[0].size();
vector<vector<int>> ans = zeroMatrix(matrix, n, m);
cout << "The Final matrix is: n";
for (auto it : ans) {
for (auto ele : it) {
cout << ele << " ";
}
cout << "n";
}
return 0;
}
CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yPHZlY3RvcjxpbnQ+PiB6ZXJvTWF0cml4KHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJm1hdHJpeCwgaW50IG4sIGludCBtKSB7CgogICAgaW50IHJvd1tuXSA9IHswfTsgLy8gcm93IGFycmF5CiAgICBpbnQgY29sW21dID0gezB9OyAvLyBjb2wgYXJyYXkKCiAgICAvLyBUcmF2ZXJzZSB0aGUgbWF0cml4OgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgICAgICBpZiAobWF0cml4W2ldW2pdID09IDApIHsKICAgICAgICAgICAgICAgIC8vIG1hcmsgaXRoIGluZGV4IG9mIHJvdyB3aWggMToKICAgICAgICAgICAgICAgIHJvd1tpXSA9IDE7CgogICAgICAgICAgICAgICAgLy8gbWFyayBqdGggaW5kZXggb2YgY29sIHdpaCAxOgogICAgICAgICAgICAgICAgY29sW2pdID0gMTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICAvLyBGaW5hbGx5LCBtYXJrIGFsbCAoaSwgaikgYXMgMAogICAgLy8gaWYgcm93W2ldIG9yIGNvbFtqXSBpcyBtYXJrZWQgd2l0aCAxLgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgICAgICBpZiAocm93W2ldIHx8IGNvbFtqXSkgewogICAgICAgICAgICAgICAgbWF0cml4W2ldW2pdID0gMDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gbWF0cml4Owp9CgppbnQgbWFpbigpCnsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gbWF0cml4ID0ge3sxLCAxLCAxfSwgezEsIDAsIDF9LCB7MSwgMSwgMX19OwogICAgaW50IG4gPSBtYXRyaXguc2l6ZSgpOwogICAgaW50IG0gPSBtYXRyaXhbMF0uc2l6ZSgpOwogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBhbnMgPSB6ZXJvTWF0cml4KG1hdHJpeCwgbiwgbSk7CgogICAgY291dCA8PCAiVGhlIEZpbmFsIG1hdHJpeCBpczogbiI7CiAgICBmb3IgKGF1dG8gaXQgOiBhbnMpIHsKICAgICAgICBmb3IgKGF1dG8gZWxlIDogaXQpIHsKICAgICAgICAgICAgY291dCA8PCBlbGUgPDwgIiAiOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICJuIjsKICAgIH0KICAgIHJldHVybiAwOwp9Cgo=