Boolean Matrix Problem

Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1.

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is r and c, r is the number of rows and c is the number of columns.
The second line of each test case contains all the elements of the matrix in a single line separated by a single space.

Output:
Print the modified array.

Constraints:
1 ≤ T ≤ 100
1 ≤ r, c ≤ 1000
0 ≤ A[i][j] ≤ 1

Example:
Input:
3
2 2
1 0

0 0
2 3
0 0 0 
0 0 1
4 3
1 0 0
1 0 0
1 0 0
0 0 0

Output:
1 1
1 0
0 0 1 
1 1 1
1 1 1
1 1 1
1 0 0

Explanation:
Test case 1: 
Since only first element of matrix has 1 (at index 1,1) as value, so first row and first column are modified to 1.

The link of this problem is https://practice.geeksforgeeks.org/problems/boolean-matrix-problem/0


Solution:

CPP

#include<iostream>

using namespace std;

int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int m;

        cin>>m;

        int n;

        cin>>n;

        int a[m][n];

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<n;j++)

            {

                cin>>a[i][j];  

            }

        }

        int visited[m][n]={0}; //Initialize an m*n array with all 0's

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<n;j++)

            {

                if(a[i][j]==1 && visited[i][j]==0)

                {

                    for(int k=0;k<n;k++)

                    {

                        a[i][k]=1;

                        visited[i][k]=1;

                    }

                    for(int k=0;k<m;k++)

                    {

                        a[k][j]=1;

                        visited[k][j]=1;

                    }

                }

            }

        }

 

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<n;j++)

            {

                cout<<a[i][j]<<" ";

            }

            cout<<endl;

        }

    }

return 0;

}


Comments

Popular posts from this blog

Floyd Warshall algorithm and it's applications.

Digits in Factorial

Design a tiny URL or URL Shortner