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
4mo

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!
Select
Add answer anonymously...

Amadeus Software Engineer interview questions & answers

A Software Engineer was asked Q. Are you comfortable working in testing?
A Software Engineer was asked Q. Write a program to print the given string in reverse.
A Software Engineer was asked Q. What is the difference between a decoder and a demux?

Popular interview questions of Software Engineer

A Software Engineer was asked Q1. What is OS scheduling?
A Software Engineer was asked Q2. Are you comfortable working in testing?
A Software Engineer was asked Q3. Write a program to print the given string in reverse.
Amadeus 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