Make Columns and Rows Zeros
Given a two-dimensional array, if any element within the array is zero, make its whole row and column zero.
Statement
Given a two-dimensional array, if any element within the array is zero, set its whole row and column zero.
Examples
For example, consider the matrix below:
Assuming the matrix starts from
(0,0)
.
There are two zeros in the input matrix at positions (1,1)
and (2,3)
. The output of this should be a matrix in which the first and second rows are zero and the first and third columns are zero.
Below is the expected output matrix:
Sample input
[ [5, 4, 3, 9],
[2, 0, 7, 6],
[1, 3, 4, 0],
[9, 8, 3, 4] ]
Expected output
[ [5, 0, 3, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[9, 0, 3, 0] ]
Try it yourself
#include <string>#include <iostream>#include <vector>#include <unordered_set>using namespace std;void MakeZeros(vector<vector<int>>& matrix) {//TODO: Write - Your - Code}
Solution
Naive solution
One naive solution is to scan the ...