logo CodeStepByStep logo

mostWater

Language/Type: C++ vector STL
Related Links:

Write a recursive function named mostWater that accepts a reference to a vector 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 vector contains fewer than 2 bars, return 0.

Function: Write a C++ function as described, not a complete program.

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.