Problem title
Difficulty
Avg time to solve

Set Matrix Zeros
Easy
30 mins
Beautiful String
Easy
18 mins
Yogesh And Primes
Moderate
20 mins
Swap Two Numbers
Easy
10 mins
First Missing Positive
Moderate
18 mins
Longest Univalue Path
Moderate
10 mins
Find Unique
Easy
--
Valid Sudoku
Moderate
40 mins
Reverse Nodes in k-Group
Hard
56 mins
Sum Of Max And Min
Easy
10 mins 358

# Set Matrix Zeros

Difficulty: EASY
Avg. time to solve
30 min
Success Rate
65%

Problem Statement

#### 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
``````

``````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?
``````
##### Sample Input 1 :
``````2
2 3
7 19 3
4 21 0
3 3
1 2 3
4 0 6
7 8 9
``````
##### Sample Output 1 :
``````7 19 0
0 0 0
1 0 3
0 0 0
7 0 9
``````
##### Explanation For Sample Input 1 :
``````For First Case - Similar to the example explained above.

For Second Case -
Only the cell (2,2) has zero. So all the elements of the second row and second column are changed to zeros.
``````
##### Sample Input 2 :
``````2
4 2
1 0
2 7
3 0
4 8
3 3
0 2 3
1 0 3
1 2 0
``````
##### Sample Output 2 :
``````0 0
2 0
0 0
4 0
0 0 0
0 0 0
0 0 0
``````   Console