GATE Computer ScienceVery hardGATE CS (PYQ pattern)

The largest rectangle in a histogram with bar heights 2, 1, 5, 6, 2, 3, 4, 1, 3 (unit widths) is found with a stack of indices; a sentinel bar of height 0 is appended at the end, and a bar is popped while its height is >= the current height (the width of a popped bar extends to the new stack top, or to the start when the stack is empty). What is the largest area, and how many pops occur?

  1. A. 12 and 9
  2. B. 10 and 9
  3. C. 10 and 8
  4. D. 8 and 10

Correct answer: B. 10 and 9

Checking each popped bar with the width between the new top and the current index, the maximum area is 10; with the >= rule every real bar is popped exactly once (9 pops).

More Programming and Data Structures (GATE) questions

Exams that ask this topic