Write a recursive method named mostWater
that accepts an array of vertical bar heights as its parameter and returns the largest rectangular area of water that could be held in a contiguous region between those bars.
For example, if the bar heights are {2, 9, 6, 2, 4, 5, 8, 3, 7, 4}
, the bars could be thought of as follows:
#
# #
# # #
# # # #
# # # # #
# # # # # # #
# # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
2 9 6 2 4 5 8 3 7 4
The largest area of water that can be made within these bars has an area of 49 (7 * 7):
#
# #
#~~~~~~~~~#~~~#
# # # #
# # # # #
# # # # # # #
# # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
2 9 6 2 4 5 8 3 7 4
If the array contains fewer than 2 bars, return 0
.