Data Structures & AlgorithmsVery hard

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

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

Correct answer: A. 16

Give each even +1 and each odd −1; equal counts is then a zero-sum subarray, found by prefix-sum hashing. Including singletons (which can never balance), checking the length parity, or testing a sum of 1 gives different totals.

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

Exams that ask this topic