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
mitadruchakraborty20005 days ago
JAVA solution in O(1) space Complexity :
void booleanMatrix(int matrix[][])   {       // code here        int m = matrix.length, n = matrix[0].length;       boolean c0 = false;       for(int i=0; i<m; i++){           for(int j=0; j<n; j++){               if(matrix[0][j]==1) c0 = true;               if(matrix[i][j]==1){                   matrix[i][0] = 2;                   matrix[0][j] = 2;               }           }       }       for(int i=1; i<m; i++){           if(matrix[i][0]==2)               Arrays.fill(matrix[i],1);       }       for(int j=0; j<n; j++){           if(matrix[0][j]==2){               for(int i=0; i<m; i++)                   matrix[i][j] = 1;           }       }              if(c0) Arrays.fill(matrix[0],1);   }
0
amanrajput41 week ago
here is below python code with constant space without using any extra space
O(1) space and O(m*n) time
 
def booleanMatrix(matrix):
    col0,row,col=False,len(matrix),len(matrix[0])
    for i in range(row):
        if matrix[i][0]==1:
            col0=True
        for j in range(1,col):
            if matrix[i][j]==1:
                matrix[i][0]=matrix[0][j]=1
    for i in range(row-1,-1,-1):
        for j in range(col-1,0,-1):
            if matrix[i][0]==1 or matrix[0][j]==1:
                matrix[i][j]=1
        if col0:
            matrix[i][0]=1
0
rajritwik2311 week ago
SOLUTION  IN JAVA
void booleanMatrix(int matrix[][])   {       // code here        int row[]=new int[matrix.length];       int colom[]=new int[matrix[0].length];       for (int i=0;i<matrix.length;i++){           row[i]=0;       }       for(int j=0;j<matrix[0].length;j++){           colom[j]=0;       }       for(int i=0;i<matrix.length;i++){           for(int j=0;j<matrix[0].length;j++){               if(matrix[i][j]==1){                   row[i]=1;                   colom[j]=1;               }           }       }       for(int i=0;i<matrix.length;i++){               for(int j=0;j<matrix[0].length;j++){                   if(row[i]==1||colom[j]==1){                        matrix[i][j]=1;               }           }       }   }
+1
v6z2nrz5xqb552qstj4qzkvyr4oxwj5pxfu1yz8n2 weeks ago
void change(int k,int l,int n1,int n2,vector<vector<int>> &res){       for(int i=0;i<n2;i++){           res[k][i]=1;       }       for(int i=0;i<n1;i++){           res[i][l]=1;       }   }   void booleanMatrix(vector<vector<int>> &matrix)   {       int n1=matrix.size();       int n2=matrix[0].size();        vector<vector<int>> res(n1,vector<int>(n2,0));     for(int i=0;i<n1;i++){         for(int j=0;j<n2;j++){             if(matrix[i][j]==1){              change(i,j,n1,n2,res);                }         }         }         matrix=res;      }
0
ldeepak113 weeks ago
Time: O(R*C)
Space: O(C)
 
C++ solution using set: 
        // Store column indexes in a set
        set<int> s;

        for (int i=0; i<matrix.size(); i++) {
            bool found=false;
            
            for (int j=0; j<matrix[i].size(); j++) {
                // Keep adding column indexes in the set, whenever we see a '1'
                if (matrix[i][j]==1) {
                    s.insert(j);
                    found=true;
                }
            }
    
            // If there is atleast one '1', then create a new row with all '1' and assign it to the current row.
            if (found) {
                vector<int> row(matrix[i].size(), 1);
                matrix[i]=row;
            }
        }
    
        /** 
         * Till this point we have taken care of only the row.
         * To handle columns, we itertate over the set, for each column index in the set, fill the entire column with '1'.
         * */
        for (int i : s) {
            for (int j=0; j<matrix.size(); j++) {
                matrix[j][i]=1;
            }
        }
0
debasishtewary51 month ago
void booleanMatrix(int matrix[][])   {       // code here        Stack<Integer> s=new Stack<>();       int n=matrix.length;       int m=matrix[0].length;       for(int i=0;i<n;i++){           for(int j=0;j<m;j++){               if(matrix[i][j]==1){               s.add(i);               s.add(j);}           }       }       while(!s.empty()){           int a=s.pop();           int b=s.pop();          for(int i=0;i<n;i++){              matrix[i][a]=1;          }          for(int j=0;j<m;j++){              matrix[b][j]=1;          }                  }   }
0
durpb12012 months ago
C++ code
set<int>OnesCol;
        set<int>OnesRow;
        for(int i = 0; i < matrix.size(); i++)
        {
            for(int j = 0; j < matrix[0].size();j++)
            {
                if(matrix[i][j]==1)
                {
                    OnesCol.insert(j);
                    OnesRow.insert(i);
                }
            }
        }
        set<int, greater<int> >::iterator itr;
        for (itr = OnesCol.begin(); itr != OnesCol.end(); itr++) {
            int i = 0;
            while(i < matrix.size())
            {
                matrix[i][*itr] = 1;
                i++;
            }
        }
        for (itr = OnesRow.begin(); itr != OnesRow.end(); itr++) {
            int j = 0;
            while(j < matrix[0].size())
            {
                matrix[*itr][j] = 1;
                j++;
            }
        }
0
sunghunet2 months ago
void booleanMatrix(vector<vector<int> >& matrix)
{
    int ncol = matrix[0].size(), nrow = matrix.size();
    vector<pair<int, int>> pairs;

    // investigate
    for (int r = 0; r < nrow; r++) {
        for (int c = 0; c < ncol; c++) {
            if( matrix[r][c] ==1 ){
                //cout << "row, col" << " " << r << " " << c << endl;
                pairs.push_back(make_pair(r, c));
            }           
        }
    }

    // put values into cells cross ones
    for (pair<int, int> pair : pairs) {
        for (int i = 0; i < ncol; i++) {
            matrix[pair.first][i] = 1;
        }

        for (int i = 0; i < nrow; i++) {
            matrix[i][pair.second] = 1;
        }
    }
}
0
yadavrky732 months ago
def booleanMatrix(matrix):   # code here    n=len(matrix)   m=len(matrix[0])   a=[0]*n   b=[0]*m   for i in range(n):       for j in range(m):           if matrix[i][j]==1:               a[i]=1               b[j]=1   for i in range(n):       for j  in  range(m):           if (a[i]==1 or b[j]==1):               matrix[i][j]=1   
0
jagannath23sahoo2 months ago
    void booleanMatrix(vector<vector<int> > &matrix)   {       vector<int> row(matrix.size()-1, -1);       vector<int> col(matrix[0].size()-1, -1);                     for(int i = 0; i<matrix.size(); i++)       {           for(int j = 0; j<matrix[0].size(); j++)           {               if(matrix[i][j] == 1)               {                   row[i] = col[j] = 1;               }           }       }       //Check row and fil       for(int i = 0; i<matrix.size(); i++)       {           if(row[i] == 1)           {               for(int j = 0; j < matrix[0].size(); j++)               {                   matrix[i][j] = 1;               }           }       }              //check col and fill       for(int i = 0; i < matrix[0].size(); i++)       {           if(col[i] == 1)           {               for(int j = 0; j<matrix.size(); j++)               {                   matrix[j][i] = 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.