Data Structures & AlgorithmsVery hard
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 goes wrong?
- A. Negative prefix sums are never found in the map.
- B. Nothing; the lookup order does not change the answer.
- C. The map grows to the wrong size, so the program runs out of memory.
- D. Each prefix matches itself, so empty ranges are counted and the answer is too large.
Correct answer: D. Each prefix matches itself, so empty ranges are counted and the answer is too large.
A prefix sum inserted before its lookup finds itself and counts an empty subarray at every index. The lookup must come first so that only earlier prefixes are matched.
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
- 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?
- Houses stand in a circle with values [5, 1, 9, 8, 4, 6, 2, 7]. Adjacent houses cannot both be robbed, and the first and last houses are…
- A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps…