Data Structures & AlgorithmsVery hard

How many contiguous subarrays of 1 2 3 4 5 6 7 8 of length at least 2 contain EXACTLY as many odd values as even values?

  1. A. 6
  2. B. 17
  3. C. 16
  4. D. 15

Correct answer: C. 16

Map odd to +1 and even to −1; equal counts give zero balance, which is the prefix-sum counting trick again. Including singletons (which can never balance), testing balance one, or testing length parity each count different sets.

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

Exams that ask this topic