Solving 2D Arrays Questions | Make zeroes | GeeksForGeeks | Nishant Chahar Ep-19

Hi guys welcome back to the channel, I am Nishant Chahar, so in today's video we are going to talk about Make Zeroes, it's a question. In this we are given a 2d array, 2d array is a matrix, so we are doing the first question with a matrix, we'll check some things in this, traversal, bounds, whenever we do a matrix question. So we'll learn all these things, question is very easy, but the concept is a little different, little new. So we will focus on learning them, let's start the video without any delay. Before starting the video, subscribe the channel if you haven't already.

And like this video, you can join the telegram channel, you can star it on GitHub, all the codes come there. So, let's start the video now. Okay, so you are given a matrix here, of nxm, your task is to make zeroes that means, in whole matrix when you find a zero, convert its upper left, lower and right value to zero, we just have to do that, like we took this matrix, let's copy it, so this is a matrix given, of nxm, here both n and m are 4, we have a matrix of 4×4, we are given, wherever there's a zero, what we will do we have to make its upper, right, lower and left into 0, and make it's value to the sum of all of them. Left + right + lower, that's what we have to do in this question, it's not hard, very easy.

So, first of all let me tell you a mistake we can make in the approach, what we will do is, go to wherever 0 will come, change everyone around it, then the next row and then next, we'll do the whole like this. There's a big flaw in this, a very big mistake, but generally when we try do this, this is the first thing that comes to mind, so we'll first try this thing, then we'll change one thing in this so the question will be solved. You know that the indexes in an array are like this, 0, 1, 2, 3.

So if we want any element, like this element, what will it's index be, it's index will be 1,1. This is x and this is y, it's row, column. So x, y. Now you can see yourself when you want to find upper, left, lower, down, upper will be, you are currently at y, you need to go above, so you will have to do y-1, if you want to go to right, you will have to do x+1, you need to go left, you have to do x-1, and if you want to do down, do y+1. We have to remember this, We need to check all these conditions, whether y-1 did it become smaller than 0, or if it's equal to 0, we have to check at right, if it's smaller than n, then we check the same greater than 0 thing at left, and at right, whether it's smaller than n.

The matrix is nxm, so this is your rows, and this is your columns. So we have to check all these conditions, or the index will go out of bound, and then it will say that this memory does not exist, so how will it be accessed, and some error will be presented. Let's code what we have understood, we are given a matrix, we will get it's n, from .size we get n, from matrix's 0th size we get the rows, let's check once more, whether, yes, so it's at least of 1 size, at least 1×1, so there's not a problem, what we have to do is, similarly, traverse in j, that is traverse in rows, so we'll do the same thing here, we traversed in i, j, then we have to check all four of them, whether the matrix of i, j is 0, so make everyone around 0, so first we'll check whether i+1 i+1 should be less than n, then we have to check if we are doing i-1, that should be greater than equal to 0, similarly we have to do the same thing for j, so we'll just change it to j, here it will be m instead of n, here we'll do j.

Next what we have to do is, we have to make that element 0, and sum, the current element of matrix which is 0, sum all of them in it. So, first we'll do that, we'll add matrix's i+1,j into matrix's i, j. Okay, easy. Then we just have to make this equal to 0, similarly, everyone else will be the same, let's quickly do that. This is done, and now it will automatically return the matrix, now let's try to run it, the answer will be wrong, certainly. See, this was expected, and this is the output it got. Now let's understand why we got this output. So this matrix was given to us, what we did is make this 0, this, this and this. And did 3+7+6+4 here, this is 20. We changed it to 20, then we went to the next one, we are traversing like this here and then there. then we found 0 here, since we were changing in the same matrix, we found 0 again, we made this 0 too and this, and made this 0 again.

And only 4, 6 and 7 were there so we changed it's value. Then we went to the next, Went here, here and then came here at 4, it had become 0, so it made this 0 too, this was 0 already, made this 0, made this into 0 too. This was 0 already, so it made this 0. Doing this, you can see all of the element became 0, and 50 will come here after sum, we did not wanted that, we just wanted, in the initial matrix if there already was 0, we just wanted to turn everyone around it to 0, for that, we have to make it's copy, in which we'll change it. Then we'll convert that copy back into matrix, or directly change into matrix, and check from the copy whether there's a 0 here or not. So what we'll do is, make a vector of vector, temp, and give it the matrix's value. Then we'll just check in temp, whether it's 0 or not, then we'll do the same thing, let's run it. Alright, your output and expected output are both the same. Then let's submit it. So this is showing wrong submission because we didn't change here, we were checking the matrix's element, which are changed, so we have to add also from the temp.

Let's try compiling it. The answer's right, submit it now. And this is now submitted. Thank you so much for watching this video, I hope you understood this question, we'll do some more questions on matrix like this, then we'll head onto the next topic, strings and then we'll study time complexity, so stay tuned for all these videos, we'll meet in the next video, until then Bye!.