Learn-dsa..in 30 days!



























CC-15 : Set matrix 0s.

Description:

If any element in the matrix is 0, we need to set all elements in the elements row and column to 0. Given input matrix mat, update and return the matrix with 0s set as described.

Test cases and expected outputs:

Input Expected outputs
mat = {
{0,1,2},
{3,4,5},
{6,7,0}};
0,0,0,
0,4,0,
0,0,0,
mat = {
{1,1,2,3},
{1,0,0,3},
{1,1,2,3},};
1,0,0,3,
0,0,0,0,
1,0,0,3,
mat = {
{9,10},
{9,10},
{0,10},
{9,10}};
0,10,
0,10,
0,0,
0,10,

Pseudocode:

The java method should accept following input parameters: mat (integer matrix).
We cannot directly search for 0s and set all other elements in its corresponding rows and columns to 0s, as this erases the information whether any other corresponding row or column element also has 0s and its rows and columns also need to be set to 0s.
So, first we need to check whether first row or first column has 0s. For this initialize 2 variables firstRow0 and firstCol0. Using for loop check first row and set firstRow0 to true if first row of mat contains any 0. Also using another for loop check first column and set firstCol0 if first column of mat has any 0s.
Next, we need to check mat using for loops, if any of the element is 0, we will set mat[rIdx][0]=0 and mat[0][cIdx]=0. This way, we have marked all the rows or columns that’s elements need to be set to 0. To do this, iterate through mat using a for loop, using variable rIdx as loop counter. Loop variable rIdx will be initialized with value 1 and will iterate through all the matrix’s columns one by one till rIdx < mat.length:
Iterate through mat using a for loop, using variable cIdx as loop counter. Loop variable cIdx will be initialized with value 1 and will iterate through all the matrix’s columns one by one till cIdx < mat[0].length:
If the current element (mat[rIdx][cIdx]==0), then we need to set mat[rIdx][0]=0; and set mat[0][cIdx]=0.
Next Iterate through mat again using for loops and set all elements where (mat[rIdx][0]==0)||(mat[0][cIdx]==0)) to 0. This way based on the marking done in previous steps we will have set the required 0s. To do this, iterate through mat using a for loop, using variable rIdx as loop counter. Loop variable rIdx will be initialized with value 1 and will iterate through all the matrix’s columns one by one till rIdx < mat.length:
Iterate through mat using a for loop, using variable cIdx as loop counter. Loop variable cIdx will be initialized with value 1 and will iterate through all the matrix’s columns one by one till cIdx < mat[0].length:
If ((mat[rIdx][0]==0)||(mat[0][cIdx]==0)), then set mat[rIdx][cIdx]=0.
Please note that last 2 steps are done using start index of 1 and not 0. This is because the first row and columns are used as markers. But we need to set first row and first column also to 0s on basis of firstRow0 and firstCol0 being 0s. This will be done in next 2 steps.
If firstRow0= true, iterate through first row using a for loop and set all elements of first row of mat to 0.
If firstCol0= true, iterate through first col using a for loop and set all elements of first col of mat to 0.
After above loops are complete, elements of mat have been set to 0 as required. Return mat.

Code:

public int[][] matSet0s(int[][] mat) throws Exception{
	boolean firstRow0=false; boolean firstCol0=false;
	for (int rIdx=0; rIdx < mat.length; rIdx++) {
		if (mat[rIdx][0]==0) {
			firstRow0=true;
			break;
		}
	}
	for (int cIdx=0; cIdx < mat[0].length; cIdx++) {
		if (mat[0][cIdx]==0) {
			firstCol0=true;
			break;
		}
	}
	for (int rIdx=1; rIdx < mat.length; rIdx++) {
		for (int cIdx=1; cIdx < mat[0].length; cIdx++) {
			if (mat[rIdx][cIdx]==0) {
				mat[rIdx][0]=0;
				mat[0][cIdx]=0;
			}
		}
	}
	for (int rIdx=1; rIdx < mat.length; rIdx++) {
		for (int cIdx=1; cIdx < mat[0].length; cIdx++) {
			if ((mat[rIdx][0]==0)||(mat[0][cIdx]==0)) {
				mat[rIdx][cIdx]=0;
			}
		}
	}
	if (firstRow0==true) {
	for (int rIdx=0; rIdx < mat.length; rIdx++) {
		mat[rIdx][0]=0;
	}
	}
	if (firstCol0==true) {
		for (int cIdx=0; cIdx < mat.length; cIdx++) {
			mat[0][cIdx]=0;
		}
	}
	return mat;
}

Click here to download and run code and test cases !