Boolean Matrix

PHOTO EMBED

Tue Feb 08 2022 09:35:30 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #2d #array #matrix #practice #booleanmatrix

class Solution
{
    //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(int matrix[][])
    {
        int r = matrix.length;
        int c = matrix[0].length;

        //using two list to keep track of the rows and columns 
        //that needs to be updated with 1.
        int row[] = new int[r];
        int col[] = new int[c];
        
        for(int i = 0; i < r; i++)
        {
            for(int j = 0; j < c; j++)
            {
                //if we get 1 in matrix, we mark ith row and jth column as 1.
                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 ith row or jth column is marked as 1, then all elements
                //of matrix in that row and column will be 1.
                if(row[i] == 1 || col[j] == 1){
                    matrix[i][j] = 1;
                }
            }
        }
    }
}
content_copyCOPY

Boolean Matrix 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

https://practice.geeksforgeeks.org/problems/boolean-matrix-problem-1587115620/1/?track=DSASP-Matrix&batchId=190