Boolean Matrix

Given a 2-dimensional boolean matrix mat of size N x M, modify the matrix such that if an element is 1, set its entire row and column to 1 i.e. if mat[i][j] = 1, then make all the elements of the ith row and the jth column as 1.

Note :
You need to make the modifications in the input matrix.

You do not need to print anything, it has already been taken care of. 
Input Format
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.

The first line of each test case contains two single-spaced integers N and M, representing the number of rows and columns of the matrix respectively.

The next N line contains M single-spaced elements (0 or 1). 
Output Format:
For each test case, the modified matrix is printed.

The output for each test case is in a separate line.
Constraints:
1 <= T <= 10
1 <= N, M <= 200

Where N and M are the number of rows and columns respectively.
CodingNinjas
author
2y

You need to make the modifications in the input matrix.
You do not need to print anything, it has already been taken care of.

CodingNinjas
author
2y
Using additional space

The main idea behind this approach is that we use two temporary arrays, row[N] and col[M], to keep a track of the rows and columns which should be set to 1. Initially, both the a...read more

CodingNinjas
author
2y
Space Optimized Approach

The idea behind this approach is to make the solution space efficient by using the first cell of every row and column as a flag. This flag would determine whether the row or co...read more

Add answer anonymously...
Toppr Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter