Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

A histogram has bar heights [2, 1, 5, 6, 2, 3, 4, 1] (each bar has width 1). What is the area of the largest rectangle that fits entirely inside the histogram?

  1. A. 10
  2. B. 12
  3. C. 24
  4. D. 11

Correct answer: A. 10

Keep a stack of increasing heights; when a bar is smaller than the stack top, pop and compute the rectangle that top bar spans. Taking the tallest bar times the full width, or a rectangle that ignores the bars on either side, gives a wrong area.

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

Exams that ask this topic