Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

How many contiguous subarrays of [3, 1, 4, 1, 5, 9, 2] have their FIRST element equal to their minimum (ties allowed)?

  1. A. 11
  2. B. 6
  3. C. 8
  4. D. 7

Correct answer: C. 8

For each start i, extend j while all values stay ≥ a[i] and count the matches a[j] = a[i] only at valid ends; a monotonic stack gives the span where a[i] is the minimum. The count is per (start, end), not per start.

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

Exams that ask this topic