Summing every single subarray to find the max? One left-to-right pass does it.
From the Wikipedia article "Maximum subarray problem"
Art: Kazusa © NEXON Games Co., Ltd. / Yostar, Inc. (demo only). Credits
Tap a stop to see what it covers
Concept 1 of 1
Kadane's algorithm scans left to right, keeping the best sum of a subarray ending at the current position as max(x, current_sum + x) and the best sum seen so far. This solves the problem in O(n) time and O(1) space. For example, in [-2, 1, -3, 4, -1, 2, 1, -5, 4] it finds [4, -1, 2, 1] with sum 6.
p. 1p. 4p. 6
Maximum subarray problem
en.wikipedia.org/wiki/Maximum_subarray_problemLicense: Wikipedia, CC BY-SA 4.0
Every reel line that states a fact quotes this material with a page number. Tap the source button on any reel to read the quote.
From the Wikipedia article "Maximum subarray problem", CC BY-SA 4.0.