Skip to content

Instantly share code, notes, and snippets.

@kpol
Created March 21, 2020 04:09
Show Gist options
  • Select an option

  • Save kpol/b5264a794539963f7d2deff477b5be03 to your computer and use it in GitHub Desktop.

Select an option

Save kpol/b5264a794539963f7d2deff477b5be03 to your computer and use it in GitHub Desktop.
using System;
public class Program
{
public static void Main()
{
int[] arr = {-2, -3, 4, -1, -2, 1, 5, -3};
var result = LargestSumContiguousSubarray(arr);
Console.Write(result);
}
public static int LargestSumContiguousSubarray(int[] arr)
{
int max = arr[0];
int currMax = arr[0];
for (int i = 1; i < arr.Length; i++)
{
currMax = Math.Max(currMax + arr[i], arr[i]);
max = Math.Max(max, currMax);
}
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment