logo CodeStepByStep logo

mostWater

Language/Type: Java arrays
Related Links:

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.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.