fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. vector<vector<int>> zeroMatrix(vector<vector<int>> &matrix, int n, int m) {
  6.  
  7. int row[n] = {0}; // row array
  8. int col[m] = {0}; // col array
  9.  
  10. // Traverse the matrix:
  11. for (int i = 0; i < n; i++) {
  12. for (int j = 0; j < m; j++) {
  13. if (matrix[i][j] == 0) {
  14. // mark ith index of row wih 1:
  15. row[i] = 1;
  16.  
  17. // mark jth index of col wih 1:
  18. col[j] = 1;
  19. }
  20. }
  21. }
  22.  
  23. // Finally, mark all (i, j) as 0
  24. // if row[i] or col[j] is marked with 1.
  25. for (int i = 0; i < n; i++) {
  26. for (int j = 0; j < m; j++) {
  27. if (row[i] || col[j]) {
  28. matrix[i][j] = 0;
  29. }
  30. }
  31. }
  32.  
  33. return matrix;
  34. }
  35.  
  36. int main()
  37. {
  38. vector<vector<int>> matrix = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
  39. int n = matrix.size();
  40. int m = matrix[0].size();
  41. vector<vector<int>> ans = zeroMatrix(matrix, n, m);
  42.  
  43. cout << "The Final matrix is: n";
  44. for (auto it : ans) {
  45. for (auto ele : it) {
  46. cout << ele << " ";
  47. }
  48. cout << "n";
  49. }
  50. return 0;
  51. }
  52.  
  53.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
The Final matrix is: n1 0 1 n0 0 0 n1 0 1 n