Data Structures & AlgorithmsVery hardCodeforces Div 2 C · ICPC regional

Heights h = [1, 8, 6, 2, 5, 4, 8, 3, 7, 9] are drawn as vertical lines at positions 0 to 9. What is the largest amount of water that two of these lines can hold together with the x-axis?

  1. A. 9
  2. B. 58
  3. C. 81
  4. D. 64

Correct answer: D. 64

Area is width times the shorter height. Always moving the shorter side inward is safe because the taller side cannot give a larger area at the same width. The best pair here gives 64; moving the taller pointer instead can skip it.

More Coding Interview Problems questions

Exams that ask this topic