Kadane’s algorithm

A game lets you claim the points from any run of consecutive turns. Some turns earn points and some lose them. Which run scores the most if you must take at least one turn?

If the previous stretch has a negative total, start fresh at this number. If its total is zero or positive, extend it.

After each card, compare the current sum with the best so far. A winning run can include a negative card. For example, 4 − 1 + 3 scores 6.

Read each card once. Time is O(n) and extra space is O(1).