using System; public class Program { // Step through this C# implementation with the same input on Algo.pw: // https://a...content-available-to-author-only...o.pw/kadane?ui=en#lang=csharp&numbers=-2,3,-5,4,-1,3,-2&mode=run&step=1 static int MaxSubarray(int[] numbers) { int size = numbers.Length; int i = 0; int total = 0; int best = 0; while (i < size) { if (total < 0) { total = 0; } total = total + numbers[i]; if (i == 0 || total > best) { best = total; } i = i + 1; } return best; } public static void Main() { int[][] examples = { new[] { -2, 3, -5, 4, -1, 3, -2 }, new[] { -8, -2, -5 }, Array.Empty() }; foreach (int[] numbers in examples) { Console.WriteLine($"[{string.Join(", ", numbers)}] => {MaxSubarray(numbers)}"); } } }