Boolean Matrix | Practice | GeeksforGeeks

Given a boolean matrix of size RxC where each cell contains either 0 or 1, modify it such that if a matrix cell matrix[i][j] is 1 then all the cells in its ith row and jth column will become 1.
Example 1:
Input:
R = 2, C = 2
matrix[][] = {{1, 0},
              {0, 0}}
Output: 
1 1
1 0 
Explanation:
Only cell that has 1 is at (0,0) so all 
cells in row 0 are modified to 1 and all 
cells in column 0 are modified to 1.

Example 2:
Input:
R = 4, C = 3
matrix[][] = {{ 1, 0, 0},
              { 1, 0, 0},
              { 1, 0, 0},
              { 0, 0, 0}}
Output: 
1 1 1
1 1 1
1 1 1
1 0 0 
Explanation:
The position of cells that have 1 in
the original matrix are (0,0), (1,0)
and (2,0). Therefore, all cells in row
0,1,2 are and column 0 are modified to 1. 

Your Task:
You dont need to read input or print anything. Complete the function booleanMatrix() that takes the matrix as input parameter and modifies it in-place.
 
Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R + C) 
 
Constraints:
1 ≤ R, C ≤ 1000
0 ≤ matrix[i][j] ≤ 1
0
ashivamsahu142 days ago
CPP 
class Solution
{   
   public:
   //Function to modify the matrix such that if a matrix cell matrix[i][j]
   //is 1 then all the cells in its ith row and jth column will become 1.
   void booleanMatrix(vector<vector<int> > &matrix)
   {
       // code here 
       
       int r = matrix.size();
       int c = matrix[0].size();
       vector<int> onesPos;
       
       for(int i = 0; i < r; i++){
           for(int j = 0; j < c; j++){
               if(matrix[i][j] == 1){
                   onesPos.push_back(i);
                   onesPos.push_back(j);
               }
           }
       }
       
       int n = onesPos.size();
       for(int i = 0; i < n; i += 2){
           int row = onesPos[i];
           int col = onesPos[i + 1];
           
           for(int j = 0; j < c; j++)
               matrix[row][j] = 1;
               
           for(int j = 0; j < r; j++)
               matrix[j][col] = 1;
           
       }
   }
};
0
sandeep55213 days ago
C++ O(1) Space Optimized Solution (Time Taken : 0.25)
void booleanMatrix(vector<vector<int> > &mat)
   {
       // code here 
       int m=mat.size(),n=mat[0].size(),r=0;
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
               if(mat[i][j]){
                   if(!i) r=1;
                   else mat[i][0]=1;
                   mat[0][j]=1;
               }
           }
       }
       for(int i=1;i<m;i++)
       for(int j=1;j<n;j++)
           if(!mat[i][j] and (mat[i][0]==1 or mat[0][j]==1)) mat[i][j]=1;
       for(int i=m-1;i>=0;i--) if(!mat[i][0] and (mat[0][0]==1 or (!i and r))) mat[i][0]=1;
       for(int i=n-1;i>=0;i--) if(!mat[0][i] and r) mat[0][i]=1;
   }
0
yashvardhanyadav84 days ago
int index1[R*C] , index2[R*C];   int row=0 , col=0;      for (int i=0; i<R; i++) {       for (int j=0; j<C; j++) {           if (matrix[i][j] == 1) {               index1[row++] = i;               index2[col++] = j;           }       }   }      for (int i=0; i<row; i++) {       for (int j=0; j<C; j++) {           matrix[index1[i]][j] = 1;       }   }      for (int i=0; i<col; i++) {       for (int j=0; j<R; j++) {           matrix[j][index2[i]] = 1;       }   }
0
buddhadebkarmakar45676 days ago
def booleanMatrix(arr):   # code here    row=set()   col=set()   n=len(arr)   m=len(arr[0])   for i in range(n):       for j in range(m):           if arr[i][j]==1:               row.add(i)               col.add(j)   for i in range(n):       for j in range(m):           if i in row:               arr[i][j]=1           if j in col:               arr[i][j]=1   return arr
0
kingabhishekdixit186 days ago
#include <bits/stdc++.h> using namespace std;
// } Driver Code Ends
class Solution{      public:   //Function to modify the matrix such that if a matrix cell matrix[i][j]   //is 1 then all the cells in its ith row and jth column will become 1.   void booleanMatrix(vector<vector<int> > &a)   {       // code here        int r=a.size();       int c=a[0].size();       queue<pair<int,int>>q;   for(int i=0;i<r;i++){       for(int j=0;j<c;j++){           if(a[i][j]==1)           q.push({i,j});       }   }   while(!q.empty()){       int x=q.front().first;       int y=q.front().second;       int p=x,p1=y;        q.pop();       while(x<r){           a[x][y]=1;           x++;       }       x=p;       while(x>=0){           a[x][y]=1;           x--;       }       x=p;       while(y<c){           a[x][y]=1;           y++;       }       y=p1;       while(y>=0){           a[x][y]=1;           y--;       }   }   }};
// { Driver Code Starts.int main() {   int t;   cin>>t;   while(t--)    {       int row, col;       cin>> row>> col;       vector<vector<int> > matrix(row);        for(int i=0; i<row; i++)       {           matrix[i].assign(col, 0);           for( int j=0; j<col; j++)           {               cin>>matrix[i][j];           }       }              Solution ob;       ob.booleanMatrix(matrix);
       for (int i = 0; i < row; ++i)       {           for (int j = 0; j < col; ++j)           {               cout<<matrix[i][j]<<" ";           }           cout<<endl;       }   }   return 0;}
 // } Driver Code Ends
+1
ashishkumargupta0542 weeks ago
void booleanMatrix(vector<vector<int> > &m)   {       unordered_set<int> row,col;       int rs = m.size();       int cl = m[0].size();       for(int i=0;i<rs;i++)       {           for(int j=0;j<cl;j++){               if(m[i][j]==1){                   row.insert(i);                   col.insert(j);                }           }       }              for( auto r : row){           for(int i=0;i<cl;i++){               m[r][i]=1;           }       }       for( auto c : col){           for(int i=0;i<rs;i++){               m[i][c]=1;           }       }   }
0
kamresh4852 weeks ago
void booleanMatrix(vector<vector<int> > &matrix)
   {
       int R=matrix.size(),C=matrix[0].size();
       vector<int>row(R,0);
       vector<int>col(C,0);
       for(int i=0;i<R;i++)
           for(int j=0;j<C;j++){
               if(matrix[i][j]==1){
                   row[i]=1;
                   col[j]=1;
               }
           }
       for(int i=0;i<R;i++)
           for(int j=0;j<C;j++)
             if(row[i]==1)
               matrix[i][j]=1;
       for(int i=0;i<C;i++)
           for(int j=0;j<R;j++)
             if(col[i]==1)
               matrix[j][i]=1;
   }
0
divakarkumar20002 weeks ago
//C++
// separate array for row and column 

void booleanMatrix(vector<vector<int> > &matrix)
    {
        int r = matrix.size();
        int c=matrix[0].size();
        vector<bool>row(r,false);
        vector<bool>column(c,false);
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if(matrix[i][j]==1)
                {
                    row[i]=true;
                    column[j]=true;
                }
            }
        }
        for(int i=0;i<r;i++)
        {
            if(row[i]==true)
            {
                for(int j=0;j<c;j++)
                {
                    matrix[i][j]=1;
                }
            }
        }
         for(int i=0;i<c;i++)
        {
            if(column[i]==true)
            {
                for(int j=0;j<r;j++)
                {
                    matrix[j][i]=1;
                }
            }
        }



0
dwivedika242 weeks ago
 //JAVA solution 
 
void booleanMatrix(int matrix[][])   {       // code here        int r=matrix.length;       int c=matrix[0].length;       boolean row=false;       boolean col=false;              for(int j=0;j<c;j++){           if(matrix[0][j]==1){               row=true;               break;           }       }       for(int i=0;i<r;i++){           if(matrix[i][0]==1){               col=true;               break;           }       }              for(int i=1;i<r;i++){           for(int j=1;j<c;j++){               if(matrix[i][j]==1){                   matrix[0][j]=1;                   matrix[i][0]=1;               }           }       }              for(int i=1;i<r;i++){           for(int j=1;j<c;j++){               if(matrix[0][j]==1 || matrix[i][0]==1){                   matrix[i][j]=1;               }           }       }                                if(row==true){           for(int j=0;j<c;j++){               matrix[0][j]=1;           }       }                   if(col==true){           for(int i=0;i<r;i++){               matrix[i][0]=1;           }       }               
-1
prakashish2 weeks ago
Java Solution 
 
class Solution
{
    class pair{
        int i, j;
        pair(int i, int j){
            this.i = i;
            this.j = j;
        }
    }
    void booleanMatrix(int matrix[][])
    {
        ArrayList<pair> list = new ArrayList<>();
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[i][j] == 1){
                    list.add(new pair(i,j));
                }
            }
        }
        for(pair p : list){
            int r = p.i;
            int c = p.j;
            for(int i=0;i<matrix[0].length;i++){
                matrix[r][i] = 1;
            }
            for(int i=0;i<matrix.length;i++){
                matrix[i][c] = 1;
            }
        }
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.