← Quiz: Kadane's Algorithm: all 13 questions
Quiz: Kadane's Algorithm · Question 10 of 13
Why does Kadane start with best = a[0] instead of best = 0?
Why does Kadane start with best = a[0] instead of best = 0?
- A.It makes no difference
- B.To make the loop shorter
- C.With best = 0, an all-negative array would wrongly return 0, the sum of an empty subarray
- D.Because a[0] is always the largest value
Answer & explanation
Correct answer: With best = 0, an all-negative array would wrongly return 0, the sum of an empty subarray
When subarrays must be non-empty, the answer for [−5, −2, −7] is −2, not 0.
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 →