-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathKadanes.cs
37 lines (34 loc) · 882 Bytes
/
Kadanes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Solution
{
class Solution
{
static long MaxSubArraySum(long[] arr)
{
long currentCount = 0;
long maxCount = Int64.MinValue;
for (int i = 0; i < arr.Length; i++)
{
currentCount += arr[i];
if (currentCount > maxCount)
{
maxCount = currentCount;
}
if (currentCount < 0)
{
currentCount = 0;
}
}
return maxCount;
}
static void Main(string[] args)
{
long[] arr = { 1, 2, 3,-5,7,8,9};
long maxSubArraySum = MaxSubArraySum(arr);
Console.WriteLine(maxSubArraySum);
}
}
}