Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

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

  1. A. 12
  2. B. 6
  3. C. 15
  4. D. 11

Correct answer: D. 11

Next smaller indices (0-based): 4→1, 2→4, 5→3, 3→4, 1→−1, so 1 + 4 + 3 + 4 − 1 = 11. Dropping the −1 gives 12, summing the next smaller values instead of indices gives 6, and 1-based indices give 15.

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

Exams that ask this topic