Data Structures & AlgorithmsHardGATE CS

Bars of unit width have heights [3, 0, 2, 0, 4, 0, 1, 3, 0, 2, 5, 1, 0, 3]. After rain, how many units of water are trapped between the bars?

  1. A. 26
  2. B. 24
  3. C. 30
  4. D. 23

Correct answer: A. 26

At each position the water level is min(max to the left, max to the right) minus its height: total trapped = 26. The sum of heights (24) is not the water.

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

Exams that ask this topic