Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE

A monotonic decreasing stack of indices is used to compute the next-greater-element array for [48, 12, 29, 6, 30, 28] (push index i after popping all stack-top indices whose value is less than arr[i]). How many indices remain on the stack after the whole array is processed?

  1. A. 6
  2. B. 4
  3. C. 2
  4. D. 3

Correct answer: D. 3

These are exactly the elements with no next-greater element to their right, 3 of them in this array.

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

Exams that ask this topic