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

What does this print? (code)

What does this print?

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

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

Indexes 0..2 get +3 and indexes 1..4 get +1, so the overlap 1..2 holds 4.

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 →