➤ Problem Link : 486B. OR in Matrix
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std; int main() { int m,n; cin>>m>>n; bool B[m][n]; unordered_set<int> row,col; row.clear(); col.clear(); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>B[i][j]; if(!B[i][j]) { row.insert(i); col.insert(j); } } } if(row.size()==m) { for(int j=0;j<n;j++) col.insert(j); } else if(col.size()==n) { for(int i=0;i<m;i++) row.insert(i); } bool flag=true; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(B[i][j] && row.find(i)!=row.end() && col.find(j)!=col.end()) { flag=false; break; } } } if(!flag) cout<<"NO"; else { bool A[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) A[i][j]=-1; } for(auto it = row.begin();it!=row.end();it++) { for(int j=0;j<n;j++) A[*it][j]=0; } for(auto it=col.begin();it!=col.end();it++) { for(int i=0;i<m;i++) A[i][*it]=0; } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(A[i][j]==-1) A[i][j]=1; } } cout<<"YES\n"; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) cout<<A[i][j]<<" "; cout<<"\n"; } } }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!