🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Kadane's Algorithm: all 13 questions

Quiz: Kadane's Algorithm · Question 9 of 13

What does this print? (code)

What does this print?

a = [3, -4, 5, -1, 2, -6, 1]
cur = best = a[0]
for x in a[1:]:
    cur = max(x, cur + x)
    best = max(best, cur)
print(best)
  1. A.5
  2. B.6
  3. C.7
  4. D.3
Answer & explanation

Correct answer: 6

The best subarray is 5, −1, 2 with sum 6. Starting from 3 would add 3 − 4 = −1, so Kadane restarts at 5.

Can you get the rest right without peeking?

Take the timed Quiz: Kadane's Algorithm quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →