Data Structures & AlgorithmsVery hard

To count subarrays with sum divisible by m, why does the equal-residue method fail for a subarray count that depends on the MAXIMUM element?

  1. A. The maximum is always divisible by m.
  2. B. Maximum is not an additive quantity, so it cannot be read from prefix residues; a monotonic stack gives the containment ranges instead.
  3. C. The residue method works for every function of the subarray.
  4. D. Maxima of subarrays are always equal to their sums.

Correct answer: B. Maximum is not an additive quantity, so it cannot be read from prefix residues; a monotonic stack gives the containment ranges instead.

Prefix sums give sums as differences; a maximum does not decompose this way. Contribution-counting with a monotonic stack handles the maximum by finding the range where each element is the largest.

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

Exams that ask this topic