Data Structures & AlgorithmsHardGATE CS · TCS NQT

For [4, 1, 3, 2, 5], each index i gets the index of its previous strictly greater element (or -1 if none). What is the sum of these values over all indices?

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

Correct answer: B. 0

Maintain a stack of candidates for previous greater: pop anything not strictly greater than the current value. The remaining top is the previous greater element.

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

Exams that ask this topic