GATE Computer ScienceVery hardGATE CS (PYQ pattern)

The stock span of day i is the number of consecutive days ending at i (including i) whose price is at most the price on day i. Prices are 100, 80, 60, 70, 60, 75, 85, 40, 90, 95, 50, 120. A stack of indices is used: pop while a[top] <= a[i]; span = i + 1 if the stack is empty else i - top; push i (0-indexed i). What is the sum of all spans, and how many pops are performed in total?

  1. A. 47 and 11
  2. B. 45 and 11
  3. C. 47 and 13
  4. D. 50 and 10

Correct answer: A. 47 and 11

The spans are [1, 1, 1, 2, 1, 4, 6, 1, 8, 9, 1, 12] (sum 47). Each index is popped at most once; 11 of the 12 indices are popped, and 12 - 11 remain on the stack.

More Programming and Data Structures (GATE) questions

Exams that ask this topic