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?
- A. The bitmask counts how many values are present in the array.
- B. The bitmask stores the maximum value seen so far.
- C. Equal prefix masks mean every value's count in between is even, so the subarray between two equal masks is valid.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Why is the minimum number of adjacent swaps to make a string palindromic NOT simply half the number of mismatched mirror pairs?
- When repeatedly deleting adjacent pairs of different letters, why does the final length have fixed parity?
- In the 'shortest block to sort so the whole array becomes sorted' problem, why does the block start at the first mismatch with the sorted…
- For an anagram search with a fixed window of length 3, why is a count comparison cheap per step?
- For the 'ramp width' problem (max j − i with A[i] ≤ A[j]), why are only prefix minima and suffix maxima worth considering?
- Why does pairing the heaviest remaining person with the lightest one (when they fit together) minimise boats when each boat carries at…