Data Structures & AlgorithmsVery hard

To count subarrays with equal numbers of even and odd values, which transformation makes the problem a zero-sum subarray problem?

  1. A. Map each even value to +1 and each odd value to −1, so balance equals zero exactly when the counts match.
  2. B. Map each value to its square root.
  3. C. Sort the array and compare the two halves.
  4. D. Keep only the even values.

Correct answer: A. Map each even value to +1 and each odd value to −1, so balance equals zero exactly when the counts match.

With +1 for evens and −1 for odds, equal counts give a sum of zero, which the prefix-sum hash counts in linear time. The other transformations lose the balance information.

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

Exams that ask this topic