{
  "slug": "kadane",
  "title": "Kadane's Algorithm",
  "category": "Arrays",
  "difficulty": "medium",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Walk left to right keeping the best sum ending here. If that running sum ever goes negative, it can only hurt what follows, so restart from the current element. Track the best window seen.",
  "code": [
    "cur = best = a[0]; start = bestL = bestR = 0",
    "for i in 1 .. n-1:",
    "    if cur < 0: cur = a[i]; start = i   else: cur += a[i]",
    "    if cur > best: best = cur; bestL = start; bestR = i"
  ],
  "example": {
    "array": [
      -2,
      1,
      -3,
      4,
      -1,
      2,
      1,
      -5,
      4
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 14,
  "steps": [
    {
      "i": 0,
      "op": "read",
      "caption": "Start: current sum and best are both -2.",
      "note": null,
      "codeLine": 1,
      "stats": {
        "cur": -2,
        "best": -2
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        "read",
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 0,
          "to": 0,
          "tone": "window"
        }
      ]
    },
    {
      "i": 1,
      "op": "read",
      "caption": "Sum was negative — restart the window at 1.",
      "note": "cur = 1",
      "codeLine": 3,
      "stats": {
        "cur": -2,
        "best": -2
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        "read",
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 0,
          "to": 0,
          "tone": "window"
        }
      ]
    },
    {
      "i": 2,
      "op": "say",
      "caption": "New best sum 1 over indices 1..1.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "cur": 1,
        "best": 1
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 1,
          "to": 1,
          "tone": "window"
        }
      ]
    },
    {
      "i": 3,
      "op": "read",
      "caption": "Extend the window: add -3.",
      "note": "cur = -2",
      "codeLine": 3,
      "stats": {
        "cur": 1,
        "best": 1
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 1,
          "to": 1,
          "tone": "window"
        }
      ]
    },
    {
      "i": 4,
      "op": "read",
      "caption": "Sum was negative — restart the window at 4.",
      "note": "cur = 4",
      "codeLine": 3,
      "stats": {
        "cur": -2,
        "best": 1
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        "read",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 1,
          "to": 1,
          "tone": "window"
        }
      ]
    },
    {
      "i": 5,
      "op": "say",
      "caption": "New best sum 4 over indices 3..3.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "cur": 4,
        "best": 4
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 3,
          "tone": "window"
        }
      ]
    },
    {
      "i": 6,
      "op": "read",
      "caption": "Extend the window: add -1.",
      "note": "cur = 3",
      "codeLine": 3,
      "stats": {
        "cur": 4,
        "best": 4
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "read",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 3,
          "tone": "window"
        }
      ]
    },
    {
      "i": 7,
      "op": "read",
      "caption": "Extend the window: add 2.",
      "note": "cur = 5",
      "codeLine": 3,
      "stats": {
        "cur": 3,
        "best": 4
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "read",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 3,
          "tone": "window"
        }
      ]
    },
    {
      "i": 8,
      "op": "say",
      "caption": "New best sum 5 over indices 3..5.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "cur": 5,
        "best": 5
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 5,
          "tone": "window"
        }
      ]
    },
    {
      "i": 9,
      "op": "read",
      "caption": "Extend the window: add 1.",
      "note": "cur = 6",
      "codeLine": 3,
      "stats": {
        "cur": 5,
        "best": 5
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        "read",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 6,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 5,
          "tone": "window"
        }
      ]
    },
    {
      "i": 10,
      "op": "say",
      "caption": "New best sum 6 over indices 3..6.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "cur": 6,
        "best": 6
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 6,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 11,
      "op": "read",
      "caption": "Extend the window: add -5.",
      "note": "cur = 1",
      "codeLine": 3,
      "stats": {
        "cur": 6,
        "best": 6
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        "read",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 7,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 12,
      "op": "read",
      "caption": "Extend the window: add 4.",
      "note": "cur = 5",
      "codeLine": 3,
      "stats": {
        "cur": 1,
        "best": 6
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        "read"
      ],
      "markers": [
        {
          "name": "i",
          "index": 8,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 13,
      "op": "settle",
      "caption": "Largest subarray sum is 6 (indices 3..6).",
      "note": null,
      "codeLine": 3,
      "stats": {
        "cur": 5,
        "best": 6
      },
      "array": [
        -2,
        1,
        -3,
        4,
        -1,
        2,
        1,
        -5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        "settled",
        "settled",
        "settled",
        "settled",
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "best",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    }
  ]
}