Iteration

You're given an array arr represents a bar graph. You can pick two bars to serve as a bucket, and must discard all the other bars. Find the maximum amount of water that you can get to pool in the bucket.

You should assume the width of each bar is 0.

Example:

arr = [1,4,3,1,1,2,1]=> returns 8\notag \texttt{arr = [1,4,3,1,1,2,1]} \\[4bp] \texttt{=> returns 8}

Example: The best possible bucket (red) can catch 8 units of water.

First Few Test Cases:

To solve this problem efficiently, you should try to do a single pass through the array. The idea is to use 2 pointers which represent the walls of a bucket. The pointers start on the left and right sides of the array.

The trick is to realize that you will never get a larger area of water by moving the taller pointer inwards, like this:

This means you should only move the shortest of the 2 pointers inwards - this is the only way to get a larger area. You can solve the problem by repeatedly moving the shortest pointer inwards, until the pointers cross.

Here's an example of moving shorter pointer inwards:

To code this up, you have to start a pointer on the left and right sides of the array. You should keep moving the shortest pointer inwards, until the 2 pointers cross. Each time you move a pointer, you have to update the max area you've seen so far maxArea. See the spoiler for details on how to compute the amount of water in a bucket.

Here's the code:

Mark as Completed:
Submits:
biggestBucket
Test your code to get an output here!
biggestBucket(
)
Test your code to get an output here!