Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CPP/arrays/1. Set Matrix Zero/Input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3 4
0 1 2 0 3 4 5 2 1 3 1 5
3 changes: 3 additions & 0 deletions CPP/arrays/1. Set Matrix Zero/Output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 0 0 0
0 4 5 0
0 3 1 0
60 changes: 60 additions & 0 deletions CPP/arrays/1. Set Matrix Zero/SetMatrixZero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>
using namespace std;


void setZeroes(vector<vector<int>> &matrix) {

//unordered_set <int> row;
//unordered_set <int> col;

int n=matrix.size();
int m=matrix[0].size();
int col0=1;

for (int i=0; i<n; i++){
if (matrix[i][0]==0) col0=0;
for (int j=1; j<m; j++){
if (matrix[i][j]==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}

for (int i=n-1; i>=0; i--){

for (int j=m-1; j>=1; j--){
if (matrix[i][0]==0 || matrix[0][j]==0){
matrix[i][j]=0;
}
}
if (col0==0) matrix[i][0]=0;
}

}

int main(){

int N;
cin >>N;
int M;
cin >>M;
vector<vector<int>> matrix(N,vector <int> (M));

for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> matrix[i][j];
}
}

setZeroes(matrix);

for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}

return 0;
}
Binary file added CPP/arrays/1. Set Matrix Zero/SetMatrixZero.exe
Binary file not shown.