A matrix on n*n size is given with 0s and 1s as values. Wherever a 1 is present make the complete row and column of that element equal to 1. At the end tell how many 1s are present till the complete operation is completed.

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

To do it in minimum time complexity, start from first index, maintain 2 variables of number of of columns and number of rows completely made 1 till now. maintain a variable num=0. Now whenever a 1 is ...read more

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...
Amadeus 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