Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 B

How many non-empty contiguous subarrays of [1, 2, 4, 3, 6, 5, 7, 8] contain equally many even and odd numbers?

  1. A. 13
  2. B. 11
  3. C. 12
  4. D. 15

Correct answer: C. 12

Map odd to -1 and even to +1; a balanced subarray is a pair of equal prefix sums, counted with a hash map.

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

Exams that ask this topic