Skip to content
RED SAGSkills
medium10 pts · first answer countsdynamic-programmingkadanedebugging

Kadane on all-negative numbers

This Kadane version returns 0 for [-3, -1, -2], but the answer should be -1 since the block cannot be empty. What is the fix?

let best = 0, cur = 0
for (const x of a) {
  cur = Math.max(0, cur + x)
  best = Math.max(best, cur)
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions