Data Structures & AlgorithmsHard

To find the longest subarray in which every value appears an even number of times, why can a parity bitmask over values be used with prefix hashing?

  1. A. The bitmask counts how many values are present in the array.
  2. B. The bitmask stores the maximum value seen so far.
  3. C. Equal prefix masks mean every value's count in between is even, so the subarray between two equal masks is valid.
  4. D. The bitmask is needed only for sorting.

Correct answer: C. Equal prefix masks mean every value's count in between is even, so the subarray between two equal masks is valid.

A mask bit flips each time its value appears, so two equal masks mean every value flipped an even number of times between them. Hashing the first position of each mask gives the longest such block.

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

Exams that ask this topic