You are viewing a single comment's thread. Return to all comments →
C#
public static List<int> maxSubarray(List<int> arr) { int sum = 0; int maxSubArrSum = 0; int maxSubSeqSum = 0; foreach (int num in arr) { if (num > 0) maxSubSeqSum += num; sum += num; if (sum > maxSubArrSum) maxSubArrSum = sum; if (sum < 0) sum = 0; } if (arr.All(num => num < 0)) { maxSubSeqSum = arr.Max(); maxSubArrSum = maxSubSeqSum; } return new List<int> { maxSubArrSum, maxSubSeqSum }; }
Seems like cookies are disabled on this browser, please enable them to open this website
The Maximum Subarray
You are viewing a single comment's thread. Return to all comments →
C#