Javatpoint Logo
Javatpoint Logo

Set Matrix Zeros in Java

In this section, we will create Java programs to set matrix elements to zero with different logics. It is the most important problem usually asked in coding round interviews.

Given a m*n matrix. If any element of the matrix is 0, set it's entire row and column to 0. Note that no extra array should be used to perform the operation.

For example, consider the following matrix.

Set Matrix Zeros in Java

In the above array, the element of the second row and second column is 0. Hence, we will set all the elements of the second row and column to 0.

Let's consider another matrix.

Set Matrix Zeros in Java

The problem can be solved in the following three ways:

  1. Brute Force Approach:Using nested loops and auxiliary space.
  2. Using Hash Table:Storing the status of rows and columns in the Hash Table.
  3. Using In-Place Hashing:Store hash in the first row and first column of the matrix.

Let's discuss the above approaches one by one.

Brute Force Approach

It is naive approach to set matrix zeros. In this approach, we traverse over the matrix and for every 0, we update the corresponding row and column to 0. The approach is very easy but it may lead to wrong answer.

Then, what's the correct solution.

Solution Steps

  1. First, create a temporary matrix of the size say, m*n and initialize it's all elements with 1.
  2. Now, scan the original matrix. If A[i][j] == 0, then set all the positions of row i and col j with 0 in the new matrix.
  3. At last, copy all elements from the temporary matrix to the original matrix and print the same to get the desired result.

Pseudo Code

In above approach, we have traversed each row and column of the corresponding cell of the matrix. The time complexity for the above approach is O(n*m(n+m)) and the space complexity is O(n*m), for storing the auxiliary matrix.

But our task is to perform the same without using extra space. The following approaches demonstrate the same.

Using HashTable

In this perform the same, we will use HashTable. First, we will create a HashTable for all rows and columns of the matrix and set them to false. If the value updated to true in any row and column, it means update that particular row and column's value to 0.

Solution Steps

  1. Create two HashTable, one for row and other for column of size M and N, respectively.
  2. Initialize all the values of row[] and col[] to false.
  3. Now iterate over the matrix and for every A[i][j] == 0, set row[i] = true and col[j] = true.
  4. After completion of the step 3, again iterate over the matrix A and for any element A[i][j], if row[i] or col[j] is true, then update element A[i][j] to 0.

Let's converts the above steps into logic.

Pseudo Code

For the above approach the time complexity is O(n*m). Because Creating and filling values in two Hash Tables [O(n+m)] + Traversing the matrix [O(n*m)] + Updating the matrix [O(n*m)]. Hence, the time complexity is O(n*m). The space complexity is O(n+m) because we are using two HashTable for storing the elements.

The above approach is also not optimized. The optimized solution can be achieved by using the in-place hashing.

Using In-Place Hashing

In this approach, we store hash for row and column in the matrix itself. We can use the first row and first column of the matrix to store the status of the rows and columns respectively. The problem will occur for the status of first row and column that can be handled using two variables.

Solution Steps

  1. Define two variables firstRow and firstColumn to store the status of the first row and first column. Set them to false.
  2. Use these row and column as hash that stores the status of that row and column.
  3. Iterate over the matrix and for every A[i][j] == 0, set A[i][0] = 0 and col[0][j] = 0.
  4. Update the values of the matrix except the first row and the first column to 0 if A[i][0] = true or A[0][i] = true for A[i][j].
  5. At last, update the values of the first row and the first column.

Let's convert the above steps into logic.

Pseudo Code

The time complexity for the above approach is O(m*n) because we have traversed over the first row and first column i.e. O(m) and O(n), traversing and updating the matrix i.e. O(m*n), and at last updating the first row and first column i.e. O(m) + O(n). Therefore, the time complexity is O(m*n). We have not used an auxiliary array for the matrix, so the space complexity is O(1).

Java Program to Set Matrix Zeros

The following Java program use extra space to set elements to zeros.

SetMatrixZeros.java

Output:

Set Matrix Zeros in Java

Let's see another Java program in which we have not used auxiliary space to set matrix elements to zeros.

SetMatrixToZero.java

Output:

Set Matrix Zeros in Java

Let's solve the same problem using Set.

SetMatrixElementsToZero.java

Output:

Set Matrix Zeros in Java

We observe that in the above approach, we have not used any extra space. So, it is an efficient approach. We must consider while setting a matrix to zeros.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA