Created
November 12, 2012 07:08
-
-
Save elicwhite/4057914 to your computer and use it in GitHub Desktop.
Calculate the maximum sum from a contiguous array of ints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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