Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

How many contiguous subarrays of [3, 1, 2, 1, 4] have their minimum equal to their first element?

  1. A. 15
  2. B. 9
  3. C. 7
  4. D. 5

Correct answer: B. 9

For each start, the valid ends run until a strictly smaller element appears (ties are fine, since the first element only needs to be ≤ the rest): 1 + 4 + 1 + 2 + 1 = 9. Requiring it to be strictly smaller gives 7, 15 counts all subarrays and 5 only singletons.

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

Exams that ask this topic