Data Structures & AlgorithmsVery hardGATE CS · DSA

How many contiguous subarrays of [7, 6, 8, 5, 9, 1, 1, 9, 1, 9] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)

  1. A. 9
  2. B. 15
  3. C. 24
  4. D. 10

Correct answer: A. 9

atMost(2) = 24 and atMost(1) = 15, so exactly two odd numbers occur in 9 subarrays. Reporting atMost(2) alone is the common slip.

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

Exams that ask this topic