🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Prefix Sums and Difference Arrays: all 13 questions

Quiz: Prefix Sums and Difference Arrays · Question 4 of 13

In the prefix-sum + hash map method, why do we start with seen = {0: 1}?

In the prefix-sum + hash map method, why do we start with seen = {0: 1}?

  1. A.To avoid dividing by zero
  2. B.So that subarrays starting at index 0 are counted
  3. C.Because Python dictionaries can't be empty
  4. D.To make the answer one bigger
Answer & explanation

Correct answer: So that subarrays starting at index 0 are counted

A subarray a[0..r] has sum k when the running total equals k, which matches the empty prefix with sum 0.

Can you get the rest right without peeking?

Take the timed Quiz: Prefix Sums and Difference Arrays quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →