Welcome back.
Let's now go ahead and code the optimum solution which we discussed in the previous video.
Now this is the previous function that we've written.
So let's just have it over there.
Now I'm going to write the other function the optimum solution over here.
And let's just call it max area.
And this function again is going to take in the array.
All right, so let me just make some space over here.
Right.
So we have the previous function above.
Now in this function again we are also going to have area initialized to zero.
So let's say area is equal to zero.
And then we are going to set I to zero and j to the last index.
Right.
So let's say let I is equal to zero.
And let's say let j is equal to array dot length.
Minus one.
So we are having the maximum width at this point.
Right.
And again we have seen that area is equal to width into height.
So now we are going through loop through until I is less than J.
Right.
So if that is getting crossed then we have already compared all the possibilities and we don't need
to go further.
So this is going to be a while loop.
Now we are going to find the height at each instance.
And then we are going to find the new area and check whether that new area is greater than what we have
currently stored into our area variable.
So let's go ahead and do that.
So let's say let height is equal to math dot min.
And it's going to be the minimum height between array at I and array at J.
Right.
All right.
So this is going to be the height.
And then the new area let new area.
Is equal to height into j minus I.
All right.
So that will give us the new area.
And then we are going to say area is equal to math dot max between the current area and the new area.
What we just now computed.
Right.
So this will give the maximum area to be stored to the area variable.
All right.
And then we just have to increment right.
So again we have to decide whether we move I or whether we move J.
Now as we discussed we have to move whichever among I or J is pointing to the lesser height.
Right.
So let's do that.
So if array at at uh let's say I.
If that is lesser.
Then arrange.
If that is the case, then we have to move I.
Right.
So if that is the case then we say I plus plus.
Now if that is not the case then we move J right.
So we move j to the left.
So we say j minus minus.
So we have to decide over here whether to move I or J.
And this will keep looping as long as I is less than j.
And then once we are out of this we just need to return area and we are done with our solution.
Now let's call this function.
All right.
So again over here we have already called this for the previous function which we have written.
Now let me just comment this.
Now we are going to call it for this new function.
Right.
So let's do that over here max area.
And again let's pass in this over here 91234 and ten.
We have seen that we should get the answer as 36 in this case right.
So let's run it.
And you can see we are getting the correct answer, which is 36.
Now let's also pass in the array which we had when we discussed it.
So let's pass that array over here we have three, seven, five, six, eight and four.
Now let's run it.
And you can see yes we are getting the answer as 21 which is the case where we take this seven and this
eight.
Right.
So seven into three gives us 21.
So yes this function is also working.
Now let's take a sample input and walk through the code and also take a look at the space and time complexity
which and again remember in the interview, walking through your code with a sample input is a very
important step.
So let's go ahead and walk through the code now.