Hi everyone, let's do this question.
You are given an integer array height of length n.
There are n vertical lines drawn such that the two end points of the ith line are I zero and I comma
height at.
I find two lines that, together with the x axis, form a container such that the container contains
the most water depth is constant across containers.
Return the area that the two lines and the x axis make of container, which can store the maximum amount
of water.
Notice that you may not slant the container.
All right, now this is the question over here.
Now let's try to understand this question.
And for this we will take an example.
Let's say the array which is given which is this array right.
The array integer array height.
Let's say that's this one over here 1563 and four.
Let's say this is the array.
Now over here we can see that this actually represents n vertical lines right.
So this is zero.
Let me write the indices over here.
This is zero.
This is one this is two.
This is three and this is four.
Now over here we can see that the end points are I comma zero and I at height at I.
So if we are taking this as I is zero then the end points will be zero comma zero right.
And it would be zero comma height at zero which is one.
So this is one line right.
So let's just write this.
So this is one line.
What about this next line.
The next line will have the indices I will be one right.
So it will be one comma zero.
And it will be one comma height at I which is five.
Right.
So one comma five.
In this way we can draw n lines right.
So 12345.
The length n is five.
So we can draw five lines five vertical lines in this manner.
So let's go ahead and do that over here.
Now the first line is zero comma zero and zero comma one.
This is zero comma zero.
Let this be the point zero comma zero.
And this is the point zero comma one.
Now this will be the point one comma zero.
And this will be the point one comma five.
Right.
So again in the similar manner over here we have two comma zero and two comma six three comma zero and
three comma three four comma zero and four comma four.
So in this way is that's how we can visually see this right.
So these are the vertical lines.
Now the question asks us to identify two lines that together with the x axis form a container such that
the container contains the most water.
Now let's take an example.
Let's say we take these two lines, this line over here and this line over here.
Now this is the x axis.
So what is the container over here we have this line.
We have this line over here.
And then we have the x axis over here.
So this is forming a container.
Now how much water can it contain.
What would be the height.
It can contain water only up to this level.
Right.
Because if if there is more water it will just spill out.
Right.
So we are forming a container where the height is one.
And this, this that is one.
This is one, right.
This is one unit which is the width.
And then the depth is constant across containers.
Right.
That means for example I'm forming a container over here.
Now this container if this is six right.
And I'm taking this line four it can take height up to four.
Right.
Because above four it will spill over.
And then the width over here is two right.
And the depth across these two is constant.
So it does not factor into checking whether this container or this container can contain maximum water.
Now we have to identify the container which will contain the maximum water.
And we have to return the area.
Now if we were to return this container over here the area would be one right.
So one into one is one.
Now if you are going to return this container, the area over here is four into two which is equal to
eight.
Now we have to identify the two lines which will form the maximum area.
And then we have to return that area.
So that is the question over here.
So we have understood the question.
Now remember in the interview when you get this question always feel free to ask clarifying questions
so that you and the interviewer are on the same page.
Now what could be a possible question you could ask?
Does the y axis count as a wall?
Now over here this is zero right.
So the y axis is actually over here right.
So this line over here is just drawn so that we can visualize that the length is measured across this
dimension.
Right.
But the y axis will pass through zero zero.
So this is the y axis.
Now we could ask is the y axis to be counted as a wall.
Because in that case I can just take this and the y axis.
Right.
So then that would give me the maximum area.
Now let's say the interviewer replies no, you cannot use the y axis.
You have to use only two of the given vertical lines which we have over here.
All right.
So that's clear now.
Now another possible question that you could ask is does a line inside the container affect the area.
What does this mean.
For example, if you are taking this line over here which is six which has height six and this line
over here which has height four, now over here, inside this I have a line.
Right.
So does this in any way affect the area.
Right.
So now there could be multiple cases.
For example if you take five and three and then you have a line inside it which is greater than it,
so does it affect it in some way.
Or again you could have this case where you have six and four, and then you have a line which is less
than these two inside it.
So let's say the interviewer replies no, it does not affect it.
Right?
If there is a line in between the lines that you have chosen, it does not affect the area in any way.
So we just don't need to consider this at all if we are taking these two lines.
All right.
So that's also clear.
Now another possible question is what about the thickness of the lines.
Right.
So if this if you're drawing a very thick line you could see that the area the possible area is reducing
right now.
What about the thickness.
Does it affect it.
And let's say the interviewer replies no, don't consider the thickness of the lines.
It should be treated as negligible.
All right.
So we have got a fair understanding of the question.
And we also have understood the constraints.
Right.
So we know that we should not consider the y axis.
We can only use the given lines as vertical lines.
And the lines inside a container is not affected.
And the thickness of the line which you are drawing is also not affecting the area.
All right.
So we've got a good understanding.
Now let's go ahead and write a few test cases.
And remember in the interview always you can ask the interviewer whether it's okay, whether both of
you together can come up with test cases.
So that will.
Solidify your understanding of the question.
And before going going ahead and solving the question, you will be very thorough with what the expected
output or the expected function has to do.
All right.
Now let's go ahead and write a few test cases.
We have already seen one test case if the given array is 15634.
This is the, uh these are the lines that we have to that we have, we can draw.
Right.
And in this case, the maximum area will be obtained if you take five and four.
Right.
So if you take five and four we can fill the container up to this level.
So the height will be four and the width will be 123.
Right.
So the area in this case will be four into three which is equal to 12.
Now how did we find this.
So that is what we have to do right now.
One possibility is you can take all the possible cases right.
So if you take one and five the area will be one into one.
If you take one and six the area will be one into two.
If you take one and three, the area will be one into three.
And if you take one and four, the area will be one into four, etc. if you take all the possible cases
and once we discuss the solution and we will discuss the brute force solution as well, you can see
that the maximum area over here will be obtained when you take five and four.
So in this case we get four into 123.
This is three right.
So this area over here will be four into three which is equal to 12.
And that will be the maximum area in this case.
Now let's go ahead and write another test case.
What if the array which is given to us is ten 65657.
Now let's visualize this.
So over here we have ten.
And then over here we have seven.
And everything in between that is less than ten and seven right.
So you have 656 and five.
Now it's very clear that the maximum area will be obtained if we take these two lines.
Right, ten and seven.
Now in this case you can see that we can fill it up to a height of seven.
And the width will be one two, three, four, five five units.
So the area will be seven into five which is equal to 35.
All right.
And again over here at this point we just writing test cases.
We're not thinking of how to solve it.
This is the expected output.
If the input is this array over here.
All right.
Now if we are getting an empty array definitely the area will be zero.
And if you are getting an array with only one element right.
So we cannot form a container.
So in this case also the area is equal to zero.
All right.
So we have a fair understanding of the question.
And we have written out test cases.
And at this point the interviewer and you are at the same page.
Now in the next video let's go ahead and think through how we can solve this question.