Complexity is O(n)
public static void kandealgo ()
{
int[] numbers = { -1, -2 };
int max_so_far = 0;
int max_end_here = 0;
boolean allnegative = false;
int max_negative_value = 0;
for (int i = 0; i < (numbers.length); i++)
{
if (numbers[i] > 0)
{
allnegative = true;
}
else
{
if (max_negative_value == 0 || max_negative_value < numbers[i])
{
max_negative_value = numbers[i];
}
}
max_end_here = max_end_here + numbers[i];
max_end_here = Math.max(max_end_here, 0);
max_so_far = Math.max(max_so_far, max_end_here);
}
if (!allnegative)
{
System.out.println("All values are negative in array , and min value is " + max_negative_value);
}
else
{
System.out.println("Max so far is " + max_so_far);
}
}