[Solved] A Boolean Matrix Problem Contest Problem

A Boolean Matrix Problem: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.

A Boolean Matrix Problem Contest

Input:
The first line of input contains T denoting the number of testcases. T testcases follow.
The first line of each testcase contains m and n denoting number of rows and number of columns.
Then next m lines contain n elements denoting the elements of the matrix.

Output:
For each testcase, in a new line, print the modified matrix.

Constraints:
1 <= T <= 100
1 <= m, n <= 900
Matij ∈ {0,1}

Examples:
Input:

2
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
1 2
0 0
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
0 0

Explanation:
Testcase1: rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1

Solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class BooleanMatrixProblem {
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next(){
            while (st == null || !st.hasMoreElements()){
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            }catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
    }
    public static void main(String[] args) {
        FastReader fr = new FastReader();
        int t = fr.nextInt();
        while (t-->0){
            int m = fr.nextInt();
            int n = fr.nextInt();

            String string1 = "";
            for(int i=0;i<n;i++)
                string1 = string1 + "1 ";

            String string2 = "";
            for(int i=0;i<n;i++)
                string2 = string2 + "0 ";

            int[][] matrixx = new int[m][n];

            int count = 0;

            ArrayList<Integer> list = new ArrayList<Integer>();

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

                for(int j=0;j<n;j++)
                {
                    matrixx[i][j] = fr.nextInt();

                    if(matrixx[i][j]==1 && count==0)
                    {
                        list.add(j);
                        count++;
                    }
                }

                if(count!=0)
                    System.out.println(string1);
                else
                    System.out.println(string2);
            }

        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *