Welcome back.
Let's now code the brute force solution which we discussed in the previous video.
Now for this we are going to write a function.
Let's call it max area brute force.
And this function is going to take in the array.
All right.
So we have array over here.
Now once inside this function we will initialize the area to be equal to zero.
All right.
And then finally we will just return this area.
We will see whether we are getting an area which is greater than what we have over here.
And accordingly we will replace the area with the greater value.
All right.
Now let's say that the area, the array which is given to us is let's say three, seven, five, six,
eight.
And for the same one which we discussed in the video previously.
All right.
Now what are we going to do.
As we discussed we are going to have a variable I and it will go from zero till this eight over here.
Right.
So let's write that over here.
So for.
Let I is equal to zero I less than array.
Dot length.
Minus one I plus plus.
So we have arrived at length minus one because we just want to go up to this element over here.
Right.
Now once we are inside this for loop we are going to have another for loop.
So let's say for let j is equal to I plus one right.
So we're going to start from the next element after I.
Right.
And then we are going to check up to j less than array dot length.
And then we are going to say J plus, plus.
All right.
Now, what are we going to do?
We're going to find the minimum height at these two indices.
Right.
So let's say the height.
Let's say let height.
Or we can say const height.
At this point, const height is equal to math dot min.
And then we're going to find the minimum right height at these two indices at I and j.
So let's say min array at I.
And RJ.
So that will give us the height.
Right.
And what about the width.
So const width.
Will be equal to.
J minus I.
Right.
So if j is equal to one and I is equal to zero, then we know that the width is zero one -0 which is
equal to one.
So that's j minus I over here.
So we have got the height and width right.
Now we can just find the area by multiplying the height and width.
And then we are going to say area is equal to math dot max because we want the maximum area right among
the current area and height dot height into width.
All right.
So this will give us the maximum area.
Right.
So the maximum area will always be stored into area.
And then we can just return area.
So that's it right.
So we're done with our function.
Now let's go ahead and test this.
Now to test it's easy.
Let's just pass this array over here because we have already discussed this in the previous video.
So the expected maximum area is 21 right.
So when we take seven and eight so that's seven into three.
So let's just call the function so max area brute force.
And we're going to pass in this array over here.
Now let's see what we get as the output.
So let's just run this.
And the output that we're getting is 21 which is correct.
Right.
So yes our function is working.
Now let's take another example.
Also let's say we are passing an array where it's obvious.
Let's say we have the largest values at the two ends.
So 9123.
And then let's say we have ten right.
So what is the expected area.
In this case it should be the height is nine right.
It should be the minimum of nine and ten.
And the width will be 123 and four.
So we should get 36 right.
So yes we are getting 36 as the output over here.
So yes this function is working.
Now let's take a sample input and walk through the code and relook at the time and space complexity.
Remember in the interview this is an important step.
Once you have written the code, you always have to take some sample input and walk through the code.
So let's go ahead and look at the code.
Walk through now.