#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> zeroMatrix(vector<vector<int>> &matrix, int n, int m) {
// int row[n] = {0}; --> matrix[..][0]
// int col[m] = {0}; --> matrix[0][..]
int col0 = 1;
// step 1: Traverse the matrix and
// mark 1st row & col accordingly:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
// mark i-th row:
matrix[i][0] = 0;
// mark j-th column:
if (j != 0)
matrix[0][j] = 0;
else
col0 = 0;
}
}
}
// Step 2: Mark with 0 from (1,1) to (n-1, m-1):
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (matrix[i][j] != 0) {
// check for col & row:
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
}
//step 3: Finally mark the 1st col & then 1st row:
if (matrix[0][0] == 0) {
for (int j = 0; j < m; j++) {
matrix[0][j] = 0;
}
}
if (col0 == 0) {
for (int i = 0; i < n; i++) {
matrix[i][0] = 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+PiB6ZXJvTWF0cml4KHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJm1hdHJpeCwgaW50IG4sIGludCBtKSB7CgogICAgLy8gaW50IHJvd1tuXSA9IHswfTsgLS0+IG1hdHJpeFsuLl1bMF0KICAgIC8vIGludCBjb2xbbV0gPSB7MH07IC0tPiBtYXRyaXhbMF1bLi5dCgogICAgaW50IGNvbDAgPSAxOwogICAgLy8gc3RlcCAxOiBUcmF2ZXJzZSB0aGUgbWF0cml4IGFuZAogICAgLy8gbWFyayAxc3Qgcm93ICYgY29sIGFjY29yZGluZ2x5OgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgICAgICBpZiAobWF0cml4W2ldW2pdID09IDApIHsKICAgICAgICAgICAgICAgIC8vIG1hcmsgaS10aCByb3c6CiAgICAgICAgICAgICAgICBtYXRyaXhbaV1bMF0gPSAwOwoKICAgICAgICAgICAgICAgIC8vIG1hcmsgai10aCBjb2x1bW46CiAgICAgICAgICAgICAgICBpZiAoaiAhPSAwKQogICAgICAgICAgICAgICAgICAgIG1hdHJpeFswXVtqXSA9IDA7CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgY29sMCA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8gU3RlcCAyOiBNYXJrIHdpdGggMCBmcm9tICgxLDEpIHRvIChuLTEsIG0tMSk6CiAgICBmb3IgKGludCBpID0gMTsgaSA8IG47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAxOyBqIDwgbTsgaisrKSB7CiAgICAgICAgICAgIGlmIChtYXRyaXhbaV1bal0gIT0gMCkgewogICAgICAgICAgICAgICAgLy8gY2hlY2sgZm9yIGNvbCAmIHJvdzoKICAgICAgICAgICAgICAgIGlmIChtYXRyaXhbaV1bMF0gPT0gMCB8fCBtYXRyaXhbMF1bal0gPT0gMCkgewogICAgICAgICAgICAgICAgICAgIG1hdHJpeFtpXVtqXSA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy9zdGVwIDM6IEZpbmFsbHkgbWFyayB0aGUgMXN0IGNvbCAmIHRoZW4gMXN0IHJvdzoKICAgIGlmIChtYXRyaXhbMF1bMF0gPT0gMCkgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbTsgaisrKSB7CiAgICAgICAgICAgIG1hdHJpeFswXVtqXSA9IDA7CiAgICAgICAgfQogICAgfQogICAgaWYgKGNvbDAgPT0gMCkgewogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIG1hdHJpeFtpXVswXSA9IDA7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBtYXRyaXg7Cn0KCmludCBtYWluKCkKewogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBtYXRyaXggPSB7ezEsIDEsIDF9LCB7MSwgMCwgMX0sIHsxLCAxLCAxfX07CiAgICBpbnQgbiA9IG1hdHJpeC5zaXplKCk7CiAgICBpbnQgbSA9IG1hdHJpeFswXS5zaXplKCk7CiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IGFucyA9IHplcm9NYXRyaXgobWF0cml4LCBuLCBtKTsKCiAgICBjb3V0IDw8ICJUaGUgRmluYWwgbWF0cml4IGlzOiBuIjsKICAgIGZvciAoYXV0byBpdCA6IGFucykgewogICAgICAgIGZvciAoYXV0byBlbGUgOiBpdCkgewogICAgICAgICAgICBjb3V0IDw8IGVsZSA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgIm4iOwogICAgfQogICAgcmV0dXJuIDA7Cn0KCg==