I have the following puzzle: Create a method to calculate the largest tanker volume that will be used to hold floodwater. Method input: a one-dimensional array of integers and one integer value that represents the width of the tanker.
integer getLargestTanker(Integer width, Integer[] values)
The one-dimensional array contains integer values that represent the tanker heights. The distance between those integer values in the array represents the tanker length.
Example
Let's say we have the following array:
{2, 9, 6, 3, 5, 7}
We need to pick two numbers such that the product of the effective height and length (distance between those two numbers) is the maximum. If we pick 2 and 9, the effective tanker height is: 2 and the distance is 1. In this example, the maximum tanker volume value would be 7x4x(static given width). Because we should choose heights: 9 and 7. Explanation: the effective height is 7 (since it's a tanker that holds water). The length of the tanker is 4 (index-1 and index-7).
My Solution
Two nested loops. Complexity is O(n^2). I strongly believe that there should be a better solution, but I couldn't come up with one. Do you have any better idea?