intermediate · dynamic programming · Kadane's algorithm · arrays

The best streak of daily profits

Problem

A shop records its profit (possibly negative) for each day. Find the largest total profit over a run of one or more consecutive days.

Constraints

  • 1 ≤ n ≤ 32
  • profits from −999 to 999

Examples

[−3, 4, −1, 2, −5, 3] → 5 — 4 − 1 + 2 = 5.

[−8, −2, −6] → −2 — At least one day must be chosen.

Show the solution

Kadane's algorithm

current = the best total of a run ending at day i. A negative current can only hurt the next day, so restart there.

Why it is correct

current(i) = max(a[i], current(i − 1) + a[i]) is the best sum of a run ending at i, because such a run is either a[i] alone or a run ending at i − 1 extended by a[i]. best is the maximum over all i, which covers every run.

Complexity

Time O(n), extra space O(1). One pass with two comparisons per day.

For contrast: Sum every run with running sums. That costs O(n²) time and O(1) space.

Edge cases

  • All negative: the answer is the largest single value, not 0.
  • One day: that value.

Common mistakes

  • Initialising best to 0, which is wrong when every profit is negative.
  • Resetting current to 0 instead of a[i].

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class MaximumSubarray
{
    /// <summary>The largest sum of a non-empty contiguous run (Kadane). O(n), O(1) space.</summary>
    public static int BestSum(int[] profits)
    {
        int best = profits[0], current = profits[0]; // current = best sum of a run ending here
        for (int i = 1; i < profits.Length; i++)
        {
            current = Math.Max(profits[i], current + profits[i]); // start fresh or extend
            best = Math.Max(best, current);
        }
        return best;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/MaximumSubarray.cs

from collections.abc import Sequence
 
 
def best_sum(profits: Sequence[int]) -> int:
    """The largest sum of a non-empty contiguous run (Kadane). O(n), O(1) space."""
    best = current = profits[0]  # current = best sum of a run ending here
    for value in profits[1:]:
        current = max(value, current + value)  # start fresh or extend
        best = max(best, current)
    return best

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/maximum_subarray.py

package dev.bekadan.dsa.problems;
 
public final class MaximumSubarray {
    private MaximumSubarray() {
    }
 
    /** The largest sum of a non-empty contiguous run (Kadane). O(n), O(1) space. */
    public static int bestSum(int[] profits) {
        int best = profits[0], current = profits[0]; // current = best sum of a run ending here
        for (int i = 1; i < profits.length; i++) {
            current = Math.max(profits[i], current + profits[i]); // start fresh or extend
            best = Math.max(best, current);
        }
        return best;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/MaximumSubarray.java

#pragma once
 
#include <algorithm>
#include <cstddef>
#include <span>
 
namespace bekadan::dsa::problems {
 
// The largest sum of a non-empty contiguous run (Kadane). Precondition: profits is not empty. O(n), O(1) space.
inline int maximum_subarray(std::span<const int> profits) {
    int best = profits[0], current = profits[0];  // current = best sum of a run ending here
    for (std::size_t i = 1; i < profits.size(); ++i) {
        current = std::max(profits[i], current + profits[i]);  // start fresh or extend
        best = std::max(best, current);
    }
    return best;
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/maximum_subarray.hpp