Data Structures & AlgorithmsHard
Counting subarrays whose sum is divisible by k with prefix remainders, which statement is TRUE?
- A. Only the last index of each remainder needs storing; first indices are never used.
- B. The count is unaffected by the seed if the array contains no zero elements.
- C. Seeding count[0] = 1 double-counts subarrays that start at index 0.
- D. The map must start with count[0] = 1, because the empty prefix has remainder 0 and pairs with any prefix of remainder 0.
Correct answer: D. The map must start with count[0] = 1, because the empty prefix has remainder 0 and pairs with any prefix of remainder 0.
The empty prefix at index 0 has remainder 0 and must be in the map, or every subarray beginning at the first element is lost. The other statements either misstate the first-index rule or claim a seed effect that depends on zeros, which is false.
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
- Two pointers count subarrays of POSITIVE integers with sum at most K. Why can the left pointer only move forward as the right pointer moves?
- When summing the MAXIMUM over all subarrays with repeated values, the standard contribution method uses one strict and one non-strict…
- 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?