It is necessary to find a solution to this problem for O (n) or O (nlogn). The Kadana algorithm does not work, even if we look for the minimum modulo sum, divide and conquer, too. Below is the code I wrote.
private double Calculate(int left, int right, out int start, out int finish) { if (right == left) { start = left; finish = left; return arr[left]; } int s1 = left, f1 = right, s2, f2, s3, f3; int middle = (left + right) / 2; double leftmin = double.MaxValue; double rightmin = double.MaxValue; double temp = 0; for (int i = middle; i >= left; i--) { temp += arr[i]; if (Math.Abs(temp) < Math.Abs(leftmin)) { leftmin = temp; s1 = i; } } temp = 0; for (int i = middle + 1; i <= right; i++) { temp += arr[i]; if (Math.Abs(temp) < Math.Abs(rightmin)) { rightmin = temp; f1 = i; } } double leftans = Calculate(left, middle, out s2, out f2); double rightans = Calculate(middle + 1, right, out s3, out f3); double res = MinByAbs(MinByAbs(leftans, rightans), leftmin + rightmin); if (res == leftans) { start = s2; finish = f2; } else if (res == rightans) { start = s3; finish = f3; } else { start = s1; finish = f1; } return res; } private double MinByAbs(double a, double b) { if (Math.Abs(a) < Math.Abs(b)) return a; return b; } arr is an array in class. For example, the code does not work if the array is {5, 6, -5, -6}