Apex › Storage & databases

Kadane's algorithm — maximum subarray

Find the contiguous run of numbers with the largest sum, in a single O(n) pass. Sweep left to right keeping the best sum ending at the current position; drop the prefix the moment starting fresh beats extending — a negative prefix never helps. Step through and watch the running total reset and the best window emerge.

More in Storage & databases