Algorithms and Data Structures · Middle · Question 47 of 75

What is the idea of Kadane's algorithm for the maximum subarray sum?

Explanation

cur = Math.max(x, cur + x), best = Math.max(best, cur). A negative running sum only hurts, so it's dropped. One pass, O(n), instead of O(n²) for all subarrays.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 47

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz