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

What does this print? (code)

What does this print?

a = [2, -1, 3, 4]
P = [0]
for x in a:
    P.append(P[-1] + x)
print(P, P[4] - P[1])
  1. A.[2, 1, 4, 8] 6
  2. B.[0, 2, 1, 4, 8] 8
  3. C.[0, 2, 1, 4, 8] 6
  4. D.[0, 2, 1, 4] 6
Answer & explanation

Correct answer: [0, 2, 1, 4, 8] 6

P[4] − P[1] is the sum of a[1..3] = −1 + 3 + 4 = 6.

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 →