Set Matrix Zeros

Given an ‘N’ x ‘M’ integer matrix, if an element is 0, set its entire row and column to 0's, and return the matrix. In particular, your task is to modify it in such a way that if a cell has a value 0 (matrix[i][j] == 0), then all the cells of the ith row and jth column should be changed to 0.

You must do it in place.

For Example:

If the given grid is this:
[7, 19, 3]
[4, 21, 0]

Then the modified grid will be:
[7, 19, 0]
[0, 0,  0]

Input Format:

The first line of the input contains a single integer ‘T’ representing the no. of test cases.

The first line of each test case contains two space-separated integers ‘N’ and ‘M’, denoting the no. of the rows and columns of the matrix.

The next 'N' lines will contain ‘M’ space separated integers representing the elements of the matrix.

Output Format:

For each test case, print the modified grid.

Print output of each test case in a separate line.

Note:

You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.

Constraints:

1 ≤ T ≤ 1000
1 ≤ m, n ≤ 1000
Σ(m * n) ≤ 2000000
-2^(31) ≤ matrix[i][j] ≤ 2^(31)-1, for all (1 ≤ i ≤ n and 1 ≤ j ≤ m).

Time Limit: 1 sec

Follow up:

Can we do better than O(m * n) space?
Using O(m + n) space is an improvement but we can still do better.
We can do it using constant memory. Can you do it?
CodingNinjas
author
2y
Brute-Force Approach

The basic idea is to maintain another boolean matrix ‘isZero’ which stores whether our resultant matrix should contain a zero at a particular index or not. We can traverse every el...read more

CodingNinjas
author
2y
Space saver

The basic idea is to keep track of each row and each column and maintain two boolean arrays ‘rowZero’ and ‘colZero’ to store whether a particular row should be filled with all zeros or not ...read more

CodingNinjas
author
2y
Super Space saver

The basic idea is to store whether a complete row and complete column should be zero or not but instead of declaring extra memory for this purpose, we are using the first row and firs...read more

Saurabh Kumar
1y
currently not working
 rows,cols = len(matrix),len(matrix[0]) arr_rows,arr_cols = [False]*rows,[False]*cols for i in range(rows): for j in range(cols): if matrix[i][j] == 0: arr_rows[i] = True arr_cols[j] = True for i in r...read more
Saurabh Kumar
1y
currently not working

rows,cols = len(matrix),len(matrix[0]) arr_rows,arr_cols = [False]*rows,[False]*cols for i in range(rows): for j in range(cols): if matrix[i][j] == 0: arr_rows[i] = True arr_cols[j] = True for i in r...read more

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