Kadane maximum subarray: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Track the best sum ending here and the best sum overall.

Keep the best sum ending here and the best seen anywhere, discarding prefixes that make an extension worse.

Open the 3D visualization

Kadane maximum subarray example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Kadane maximum subarray

Largest contiguous gain or score interval.

Choosing an alternative

Subset sum may select nonadjacent items; Kadane cannot.

Worked example: input and state changes

Example input

-2, 1, -3, 4, -1, 2, 1, -5, 4

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "active": []
    }
  2. Step 3: Best ending here: -2; best overall: 1.

    State at this checkpoint
    {
      "values": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "active": [
        2
      ]
    }
  3. Step 5: Best ending here: 3; best overall: 4.

    State at this checkpoint
    {
      "values": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "active": [
        4
      ]
    }
  4. Step 8: Best ending here: 1; best overall: 6.

    State at this checkpoint
    {
      "values": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "active": [
        7
      ]
    }
  5. Step 10: Maximum subarray sum = 6.

    State at this checkpoint
    {
      "values": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "active": []
    }

Expected final state

Maximum subarray sum = 6.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(n).

Space complexity: O(1) recurrence state.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

2–24 comma-separated integers from -99 to 99.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

For a nonempty all-negative input, the best answer is the largest element, not zero.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
1
2
3
4
5
6
7
8
9
// Requires a nonempty array. Returns the maximum nonempty contiguous subarray sum. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n) {
    let current = a[0]; let best = a[0];
    for (let i = 1; i < n; i++) {
        if (current + a[i] > a[i]) { current += a[i]; } else { current = a[i]; }
        if (current > best) { best = current; }
    }
    return best;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.