Matrix Transformation Problem Statement

Given a 2-dimensional matrix of size NxN containing 0s and 1s, the task is to modify the matrix such that, if an element is 1, set its entire row and column to 1. In the end, determine how many 1s are present after completing the operation.

Explanation:

You need to modify the input matrix according to the described transformation. The counting of 1s should be done after all modifications are made.

Input:

The first line contains an integer 'T', representing the number of test cases. For each test case, the first line contains two integers N and M, where N is the number of rows and M is the number of columns of the matrix. The following N lines for each test case contain M integers (either 0 or 1) separated by spaces, representing the matrix.

Output:

For each test case, output the modified matrix with each row printed on a separate line. Each test case output is separated by a new line.

Example:

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

1 1 1 1
1 0 0 1
1 0 0 1
1 0 0 1

Constraints:

  • 1 <= T <= 10
  • 1 <= N, M <= 200

Note:

You need to make the modifications in the input matrix without using extra space for another matrix. You do not need to print anything in the function; just implement the logic and return the result.
AnswerBot
1d

Modify a matrix by setting entire row and column to 1 if an element is 1, then count the number of 1s.

  • Iterate through the matrix to find elements with value 1

  • Use two arrays to keep track of rows and c...read more

Help your peers!
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

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