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?
- A. The maximum is always divisible by m.
- B. Maximum is not an additive quantity, so it cannot be read from prefix residues; a monotonic stack gives the containment ranges instead.
- C. The residue method works for every function of the subarray.
- 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.
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
- In the 'longest window with equal numbers of high and low values' problem, what turns the condition into a standard prefix-sum problem?
- When two consecutive windows of length 3 are compared, why does equal sums reduce to a single element comparison?
- Why does a pair (i<j) with A[i] AND A[j] = 0 require disjoint sets of set bits?
- Among pairs with A[i] ≠ A[j] and A[i] XOR A[j] = 0, how many exist?
- Why can the number of distinct OR values over all subarrays of an array be small even for long arrays?
- In the array [2, 5, 5, 3, 5, 2] the maximum of a subarray is taken. How many contiguous subarrays have their maximum value occurring…