Zero Matrix

You are given a matrix 'MATRIX' of dimension 'N' x 'M'. Your task is to make all the elements of row 'i' and column 'j' equal to 0 if any element in the ith row or jth column of the matrix is 0.

Note:

1) The number of rows should be at least 1.

2) The number of columns should be at least 1.

3) For example, refer to the below matrix illustration: 

altImage

Input Format:
The first line of the input contains an integer 'T' denoting the number of test cases. 

The first line of each test case contains two space-separated integers, 'N' and 'M', as described in the problem statement.

The next 'N' lines of each test case contain 'M' integers separated by spaces describing rows of the matrix.
Output Format:
For each test case, return 'N' rows consisting of 'M' integers representing the matrix.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= N <= 100
1 <= M <= 100
-10^9 <= MATRIX[i][j] <= 10^9

Where 'MATRIX[i][j]' denotes the matrix element.

Follow Up:

Can you solve it with the space complexity of O(1)?

Time limit: 1 sec
AnswerBot
1y

The task is to modify a given matrix such that if any element in a row or column is 0, then make all elements in that row and column 0.

  • Iterate through the matrix and keep track of rows and columns tha...read more

Help your peers!
Add answer anonymously...
Goldman Sachs Software Engineer Intern 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