Hi, everyone, welcome to this new video.
So in this video, we are going to write the code for this problem, make some off island.
And as discussed, what we will do, we are going to modify the code for the number of islands problem.
We just need to maintain one variable area and will take maximum of all the areas.
Right.
That's already need to do so.
Let's copy that code and let's start modifying that code.
So first, let us copy this DFS code.
So this is the call for DFS and let's copy this one also.
Right.
So that's it.
Now what you need to do.
So right, so what you need to do, first of all, we need to do some changes here and what changes
so here what I'm returning I'm returning the number of connected components, but I need to return the
make some media.
So let's change this variable to one.
I will return answer, right, and similarly, let's remove it from here and let's return our answer
answer will be to make some area.
So let's do that.
So I'm going to return the maximum area.
Let's name this variable max area rate.
So we are not counting the number of connected components.
Let's remove going from here.
And we are going to return the mix media, right?
We are going to return maximum area.
Also, one more thing to notice here is this time the grid is basically of type integer.
Late in previous problem, grid was of type character.
So that's why I have written character here.
Now let's modify it to integer.
So what else do we need to do, what I said is, while finding out the connected component, so in this
loop, what I am doing, I am calling DFS, that means I am finding out one connected component since
I am finding out connected component.
Let's also find the area for this.
Right.
Let's say initially Area zero and Tulu, let's also pass the area because we want to find out the area
for this.
Right.
So what this DFS function will do, it will visit the connected component and it will also find out
the area and what we need to do.
We need to find out the myxoma area.
So make some area will be the maximum of the current maximum area and the area for this connected component.
Right.
So we are passing area here, so we need to pass area.
And we need to pass a.D.A head.
And what else so whenever you are visiting the element, that means you will do area plus since you
are changing the value of area here, you are changing the value of area here.
So that means you need to pass area by reference so that those changes can appear here.
Right.
So let me try to explain you what is happening here.
See what I am doing.
I need to find out the make some area.
Right.
Make some area of all connected components.
So what I am doing is whenever I am calling DFS, so what DFS will do, it will visit one connected
component based DFS will visit one connected component.
So I am passing one variable area so that DFS will update the area rate.
I am passing variable area and usually it is zero.
I am passing variable area.
So whenever DFS will visit any element, what it will do, it will do area plus plus Veidt.
So when DFS will visit this element, this connected component, so area will become full.
And since you are changing the value of area, so you need to pass area by reference because what we
need to do, we need to take maksym off all the areas.
We need to find the maximum of all the areas that you need to pass area by reference so that the changes
that you are doing here should be reflected here.
So I think that is the complete code.
So let's submit our code.
Let's first run our code and then we will submit.
So we are getting wrong answer for this, OK?
My output is zero and the expected output is six.
OK, so what we did wrong.
This is area and I'm taking makes media is mix of the mix of media and area, this seems correct to
me.
OK, so what you did wrong here is, see, this is what integer array and what I'm doing, this is character,
right?
This should be integer.
This should be one.
And similarly, instead of zero here character zero, you will compare it to zero rate.
Yep.
So let's try again.
Yes, it's working, it's now.
Yes, so basically our goal is working.
And let me try to explain to you again what we are doing here.
See what I am doing here.
I need to find out.
The maximum aerofoil can include component, because that will be my answer.
But this function, does this function visit one connected component?
And I need to find out the area for all connected component, because we need to take Maksym off all
the areas and how we can find the area for one connected component.
So whenever you are visiting one element, that element will be the part of your connected component.
So you will simply do area placeless and that's it.
That nothing else.
Very, very simple code.
So what will be the time?
Complexity.
So you are visiting each and every element.
Only once certain complexity will be eminent doing.
And what is the space complexity, space complexities.
You are creating this visit.
So space complexities, eminent domain and that.
So this is the time and the space complexity for this problem.
So if you have any doubt in this problem, do ask me.
This is all about this device.
I will see you in the next one.
Thank you.