Data Structures & AlgorithmsHard
Why is a sum of XORs over subarrays NOT equal to the sum of the subarray sums?
- A. XOR of a subarray is always zero.
- B. XOR does not distribute over addition: carries in addition have no counterpart in XOR, so the two totals differ.
- C. Subarray sums are always smaller than their XORs.
- D. Both operations are identical on non-negative integers.
Correct answer: B. XOR does not distribute over addition: carries in addition have no counterpart in XOR, so the two totals differ.
The XOR of a block drops the carries that addition would propagate, so the two quantities are different functions of the block. A direct computation of each subarray's XOR is needed.
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
- When counting subarrays with sum divisible by m, why does the prefix-remainder method count pairs of equal remainders?
- To count ordered pairs (i, j), i ≠ j, with A[i] + A[j] ≡ 0 (mod 5), which correction turns an unordered-pair count into the ordered one?
- When counting subarrays, why do two blocks with identical values but different positions count as two different subarrays?
- In the 'shortest window with a then b in order' problem, why does taking the nearest later 'b' for each 'a' suffice?
- Why is the maximum product of three values sometimes achieved by two large negatives and one large positive?
- For a binary string with no three equal consecutive bits, why is a run-length description convenient for counting?