🎙 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 5 of 13

What does this print? (code)

What does this print?

d = [0] * 6
for l, r, v in [(0, 2, 1), (1, 4, 3)]:
    d[l] += v
    d[r + 1] -= v
run, out = 0, []
for i in range(5):
    run += d[i]
    out.append(run)
print(out)
  1. A.[1, 3, 4, 3, 3]
  2. B.[1, 4, 4, 3, 3]
  3. C.[1, 4, 4, 3, 0]
  4. D.[1, 1, 1, 3, 3]
Answer & explanation

Correct answer: [1, 4, 4, 3, 3]

Indexes 0..2 get +1 and indexes 1..4 get +3, so the values are 1, 4, 4, 3, 3.

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 →