Data Structures & AlgorithmsVery hard
When summing the MAXIMUM over all subarrays with repeated values, the standard contribution method uses one strict and one non-strict comparison. Why?
- A. It is a speed trick and gives the same total with both sides strict.
- B. Because equal values are never the maximum of any subarray.
- C. To make the sum of the minimums and the sum of the maximums equal.
- D. So each subarray is credited to exactly one position of its maximum; otherwise equal maxima are counted twice or not at all.
Correct answer: D. So each subarray is credited to exactly one position of its maximum; otherwise equal maxima are counted twice or not at all.
With equal maxima, both positions would claim the same subarray unless one side breaks ties. Using a strict comparison on one side and a non-strict one on the other assigns every subarray to its leftmost or rightmost maximum exactly once.
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
- Which statement about subarrays with XOR equal to 0 is CORRECT?
- A zero-sum subarray counter keeps a map of prefix sums. At each index the current prefix is inserted into the map BEFORE the lookup. What…
- For pairs i < j with (ai + aj) divisible by an EVEN number m, with remainder counts cr, which formula is correct?
- Which statement about counting subarrays with EXACTLY k distinct values is correct?
- Positive integers, two pointers count subarrays with product less than K. Why is right − left + 1 added for each right end?
- Which statement about inversion counting with repeated values is correct?