🎙 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 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?

  1. A.It makes no difference
  2. B.To make the loop shorter
  3. C.With best = 0, an all-negative array would wrongly return 0, the sum of an empty subarray
  4. 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 →