← Quiz: Kadane's Algorithm: all 13 questions
Quiz: Kadane's Algorithm · Question 1 of 13
What does this print? (code)
What does this print?
a = [3, -4, 5, -1, 2]
cur = best = a[0]
for x in a[1:]:
cur = max(x, cur + x)
best = max(best, cur)
print(best)
- A.5
- B.6
- C.7
- D.3
Answer & explanation
Correct answer: 6
The best subarray is [5, −1, 2] with sum 6; at −4 the running sum drops to −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 →