Data Structures & AlgorithmsHardGATE CS · DSA

Lines of heights [6, 9, 5, 6, 8, 1, 8, 3, 5] stand at x = 0, 1, …, 8. Two pointers find the pair of lines that, with the x-axis, holds the most water (area = min(h[i], h[j]) × (j − i)). What is that maximum area?

  1. A. 39
  2. B. 40
  3. C. 14
  4. D. 72

Correct answer: B. 40

Start with the widest pair and always move the pointer at the shorter line inward (the taller one cannot do better with a narrower width). The best area is 40.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic