Set Matrix Zeros in Java

 


If you are a programmer, you must have written programs for traversing over a matrix.

The Set Matrix Zeros problem is based on it. There are a lot of programmers who are not able to understand the concept of set matrix zeros due to which they face problems in solving the question.  


Why would you want to spend time browsing on this much-talked-about topic of matrix? We compiled everything at one place.


About Set Matrix Zeros in Java

The Set Matrix Zeros problem is based on updating the rows and columns of the matrix to zeros. In this problem, you will be given a matrix where some of its elements will contain zero. Now, you will have to iterate over the rows and columns of that matrix which is containing zero, and will have to update its value to zero. 


Let us understand it with the help of an example. 


Example 1

For the given matrix, set matrix zeros. 

1 1 1

1 0 1

1 1 1


Solution: The updated matrix will look like this: 


1 0 1

0 0 0

1 0 1


Explanation: If you can see here, the matrix is containing the element zero in the location of arr[1][1]. As per the problem, we have to set all the rows and columns which are connected to it. Thus, it will be arr[0][1], arr[1][0], arr[1][2], and arr[2][1]. If you have studied the concept of the matrix, then you might be able to understand that we are updating the entire row and column of the given element of 0. 


Example 1

For the given matrix, set matrix zeros. 

0 1 1

1 0 1

1 1 1


Solution: The updated matrix will look like this: 


0 0 0

0 0 0

0 0 1


Explanation: We have explained to you how we are updating the values of the rows and columns to zero. Here we are doing the same thing. For the given matrix, there are two zeros present. The first one is at the location of arr [0][0] and the second one is at the arr [1][1]. We have to change the whole rows and columns for both zero elements. 


Thus, we will start updating the rows and columns for the first element. Once we are done with it, then we will do the second one. You can see the solution to how our matrix will look like. We can also write it like this: [[0,0,0],[0,0,0],[0,0,1]]. 


How To Solve Set Matrix Zeros in Java

We hope that you are now able to understand what the problem of set matrix zeros is. Let’s see how we are going to solve the question. 

Problem Statement 

For the given matrix, set matrix zeros. 

1 1 1

1 0 1

1 1 1


Solution: The updated matrix will look like this: 


1 0 1

0 0 0

1 0 1


Approach

  • First of all, we will start our iteration and will traverse the matrix. 

  • After traversing over the matrix, we will check for the row and column whether it contains zero elements or not. We are doing it to check which rows and columns we will have to update. 

  • We will initialize another matrix with size i*j and update all the elements to 1. 

  • After doing it, traverse over the original array. Now, start finding the element which is having 0. 

  • Whenever we will get the zero, then we will update the value to zero for that column and row to the duplicate matrix which we have initialized previously. 

  • Once we are done with the traversal, then we will copy the zero value of the duplicate matrix and will paste it into the original one to print the solution. 

  • We will copy the zero elements from the duplicate to the original by checking for the elements in each iteration. Once we get any 0 that is in the duplicate one, then we will update it to the original matrix.


Now, let’s check how it will work on the dry run. 


Dry Run 

  • We have initialized our iteration, then we will start traversing over the matrix. 

  • Now, we have got the first element which is 1. After it, we will keep on traversing over the matrix till the end. The elements that we have got in this iteration are 1, 1, 1. 

  • Let’s start with the next iteration. In the next iteration, we will get the first element which is 1. 

  • After it, once we have again iterated, then we have got the 0 elements. Now, we will have to update the value of the whole row and column to 0. We will do it in the duplicated matrix. We are doing it in a duplicated matrix because there are chances that the matrix might also contain the other zero too. Thus, we will update only the duplicated matrix. 

  • Now, we will keep on traversing the matrix. We will keep on iterating as there might be others 0’s too in the matrix. 

  • After doing it till the end, we will check if there is any other 0. As there are no more zeros, then we will start copying all the zeros of the duplicated matrix to the original matrix. 

  • Now, we can do it by matching each element of the duplicate to the original matrix. Once we have done it, then we will print the original matrix.  

  • The matrix that we will get is: [[0,0,0],[0,0,0],[0,0,1]].

  • After this, we got our answer. 


By following the approach, you can easily get the answer. Now, code the problem in Java or any preferred language. After solving the question, you can easily attempt the frog jumps problem


The frog jumps problem is also based on traversal over the array, so you can easily attempt it after understanding the problem statement.  


Conclusion

For every programmer, it is important to understand the problem statement. Once you have got the problem statement, then you will be able to know about the approach. 


We hope that you have understood the set matrix zeros problem, you can code the problem to enhance your coding skills.



Comments

Popular posts from this blog

What are the 5 types of inheritance?

Remove duplicates from a sorted linked list

How often can you Buy and Sell the same stock?