Find max contiguous subarray sum for array with values 1..6 and one negative.
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, -6 };
int best = a[0], cur = a[0];
for (int k = 1; k < a.Length; k++) {
cur = Math.Max(a[k], cur + a[k]);
best = Math.Max(best, cur);
}
Console.WriteLine(best);
}
}
Try solving on your own first, then reveal the official answer.
Kadane's algorithm for max subarray.