Data Structures & AlgorithmsHardGATE CS · DSA

Stock prices over 8 days are [21, 87, 49, 49, 28, 34, 81, 65]. The span of day i is the number of consecutive days ending at day i (inclusive) with price ≤ the price on day i. What is the sum of all 8 spans?

  1. A. 15
  2. B. 16
  3. C. 8
  4. D. 14

Correct answer: A. 15

Spans: [1, 2, 1, 2, 1, 2, 5, 1], sum 15. Equal prices count (≤), so using a strict < comparison gives 14 instead.

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

Exams that ask this topic