Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7] contain exactly 2 odd numbers?

  1. A. 8
  2. B. 10
  3. C. 12
  4. D. 15

Correct answer: A. 8

Track the number of odd values with a prefix count; a subarray with exactly k odds corresponds to a pair of prefixes whose odd counts differ by k.

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

Exams that ask this topic