Data Structures & AlgorithmsVery hardGATE CS · DSA

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

  1. A. 11
  2. B. 26
  3. C. 16
  4. D. 10

Correct answer: D. 10

atMost(2) = 26 and atMost(1) = 16, so exactly two odd numbers occur in 10 subarrays. Reporting atMost(2) alone is the common slip.

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

Exams that ask this topic