#include <bits/stdc++.h>
using namespace std;
void markRow(vector<vector<int>> &matrix, int n, int m, int i) {
// set all non-zero elements as -1 in the row i:
for (int j = 0; j < m; j++) {
if (matrix[i][j] != 0) {
matrix[i][j] = -1;
}
}
}
void markCol(vector<vector<int>> &matrix, int n, int m, int j) {
// set all non-zero elements as -1 in the col j:
for (int i = 0; i < n; i++) {
if (matrix[i][j] != 0) {
matrix[i][j] = -1;
}
}
}
vector<vector<int>> zeroMatrix(vector<vector<int>> &matrix, int n, int m) {
// Set -1 for rows and cols
// that contains 0. Don't mark any 0 as -1:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
markRow(matrix, n, m, i);
markCol(matrix, n, m, j);
}
}
}
// Finally, mark all -1 as 0:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == -1) {
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;
}
CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBtYXJrUm93KHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJm1hdHJpeCwgaW50IG4sIGludCBtLCBpbnQgaSkgewogICAgLy8gc2V0IGFsbCBub24temVybyBlbGVtZW50cyBhcyAtMSBpbiB0aGUgcm93IGk6CiAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgIGlmIChtYXRyaXhbaV1bal0gIT0gMCkgewogICAgICAgICAgICBtYXRyaXhbaV1bal0gPSAtMTsKICAgICAgICB9CiAgICB9Cn0KCgp2b2lkIG1hcmtDb2wodmVjdG9yPHZlY3RvcjxpbnQ+PiAmbWF0cml4LCBpbnQgbiwgaW50IG0sIGludCBqKSB7CiAgICAvLyBzZXQgYWxsIG5vbi16ZXJvIGVsZW1lbnRzIGFzIC0xIGluIHRoZSBjb2wgajoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaWYgKG1hdHJpeFtpXVtqXSAhPSAwKSB7CiAgICAgICAgICAgIG1hdHJpeFtpXVtqXSA9IC0xOwogICAgICAgIH0KICAgIH0KfQoKdmVjdG9yPHZlY3RvcjxpbnQ+PiB6ZXJvTWF0cml4KHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJm1hdHJpeCwgaW50IG4sIGludCBtKSB7CgogICAgLy8gU2V0IC0xIGZvciByb3dzIGFuZCBjb2xzCiAgICAvLyB0aGF0IGNvbnRhaW5zIDAuIERvbid0IG1hcmsgYW55IDAgYXMgLTE6CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgICAgICBpZiAobWF0cml4W2ldW2pdID09IDApIHsKICAgICAgICAgICAgICAgIG1hcmtSb3cobWF0cml4LCBuLCBtLCBpKTsKICAgICAgICAgICAgICAgIG1hcmtDb2wobWF0cml4LCBuLCBtLCBqKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICAvLyBGaW5hbGx5LCBtYXJrIGFsbCAtMSBhcyAwOgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgICAgICBpZiAobWF0cml4W2ldW2pdID09IC0xKSB7CiAgICAgICAgICAgICAgICBtYXRyaXhbaV1bal0gPSAwOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBtYXRyaXg7Cn0KCmludCBtYWluKCkKewogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBtYXRyaXggPSB7ezEsIDEsIDF9LCB7MSwgMCwgMX0sIHsxLCAxLCAxfX07CiAgICBpbnQgbiA9IG1hdHJpeC5zaXplKCk7CiAgICBpbnQgbSA9IG1hdHJpeFswXS5zaXplKCk7CiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IGFucyA9IHplcm9NYXRyaXgobWF0cml4LCBuLCBtKTsKCiAgICBjb3V0IDw8ICJUaGUgRmluYWwgbWF0cml4IGlzOiBuIjsKICAgIGZvciAoYXV0byBpdCA6IGFucykgewogICAgICAgIGZvciAoYXV0byBlbGUgOiBpdCkgewogICAgICAgICAgICBjb3V0IDw8IGVsZSA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgIm4iOwogICAgfQogICAgcmV0dXJuIDA7Cn0KCg==