← 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)
- A.[3, 4, 4, 1, 1]
- B.[3, 3, 3, 1, 1]
- C.[3, 1, 0, -3, 0]
- 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 →