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?
- A. 12 and 9
- B. 10 and 9
- C. 10 and 8
- 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).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- void insert(Stack s, int x) { if (empty(s) || top(s) <= x) { push(s, x); return; } int y = pop(s); insert(s, x); push(s, y); } void…
- Kahn's topological sort uses a FIFO queue seeded with all vertices of in-degree 0 in increasing order; vertices are dequeued, and…
- An undirected graph has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-1, 2-7, 7-8, 8-9, 9-7, 4-10, 10-11. Vertices are coloured by the parity of their…
- int range(Node n, int lo, int hi) { if (!n) return 0; int c = (lo <= n->k && n->k <= hi); if (lo < n->k) c += range(n->l, lo, hi); if…
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 22, 33, 38 are inserted in this order into an empty BST, and every…
- bool f(Node n) { if (!n) return true; if (n->l && n->l->k > n->k) return false; if (n->r && n->r->k < n->k) return false; return f(n->l)…