Zero Matrix Problem Statement

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.

Input:

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:

For each test case, return 'N' rows consisting of 'M' integers representing the matrix.

Example:

Example: Consider the MATRIX below:

Input: MATRIX = [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
Output: [[1, 0, 3], [0, 0, 0], [7, 0, 9]]

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 100
  • 1 ≤ M ≤ 100
  • -109 ≤ MATRIX[i][j] ≤ 109 - where MATRIX[i][j] denotes the matrix element.

Note:

  • You don\'t need to print anything, it has already been taken care of. Just implement the given function.

Follow-Up:

Can you solve it with a 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

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