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 [41, 35, 17, 34, 4, 37, 29, 44] (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. 0
  2. B. 8
  3. C. 2
  4. D. 1

Correct answer: D. 1

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

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

Exams that ask this topic