Boolean Matrix Transformation Challenge

Given a 2-dimensional boolean matrix mat of size N x M, your task is to modify the matrix such that if any element is 1, set its entire row and column to 1. Specifically, if mat[i][j] = 1, then make all elements of the i-th row and j-th column equal to 1.

Note:

You are required to modify the input matrix in-place.

There is no need to print anything; the output handling is already managed.

Input:

The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case consists of two spaced integer values N and M indicating the matrix dimensions.
The following N lines for each test case contain M spaced integers (0 or 1) representing the matrix elements.

Output:

For every test case, display the modified matrix. Each test case output should be shown on a new line.

Example:

Input:
2
2 2
0 1
0 0
3 3
1 0 0
0 0 0
0 0 0
Output:
1 1
0 1
1 1 1
1 0 0
1 0 0

Constraints:

  • 1 <= T <= 10
  • 1 <= N, M <= 200
  • N and M represent the number of matrix rows and columns, respectively.
AnswerBot
4mo

Modify a boolean matrix such that if any element is 1, set its entire row and column to 1.

  • Iterate through the matrix to find elements with value 1

  • Store the row and column indices of these elements

  • Upda...read more

Help your peers!
Select
Add answer anonymously...

Quess Software Engineer interview questions & answers

A Software Engineer was asked 6mo agoQ. What are the responsibilities of a Data Analyst?
A Software Engineer was asked 10mo agoQ. Explain the collection framework and its uses.
A Software Engineer was asked 10mo agoQ. Explain Spring Boot and its annotations.

Popular interview questions of Software Engineer

A Software Engineer was asked 6mo agoQ1. What are the responsibilities of a Data Analyst?
A Software Engineer was asked 10mo agoQ2. Explain the collection framework and its uses.
A Software Engineer was asked Q3. Boolean Matrix Transformation Challenge Given a 2-dimensional boolean matrix mat...read more
Quess Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits