Skip to content

Instantly share code, notes, and snippets.

@TheSavior
Created November 12, 2012 07:08
Show Gist options
  • Save TheSavior/4057914 to your computer and use it in GitHub Desktop.
Save TheSavior/4057914 to your computer and use it in GitHub Desktop.
Calculate the maximum sum from a contiguous array of ints
class Program
{
static void Main(string[] args)
{
int[] nums = new int[] { 1, 4, -6, 2, 4, -1, 3 };
bigSum(nums);
return;
}
public static void bigSum(int[] array)
{
int max = int.MinValue;
for (int i = 0; i < array.Length; i++)
{
int innerMax = 0;
for (int j = i; j < array.Length; j++)
{
innerMax += array[j];
if (array[j] > innerMax)
{
break;
}
max = Math.Max(max, innerMax);
}
}
Console.WriteLine(max);
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment