# Java Solution

• This problem is quite simple, actually question specifically asked about continous maximum sub array sum but you had to take atleast one element in it.

We had taken a flag value and will set it to 1 when we encounter positive value. In this case we would add the element value in sum and would compare with our min value, if the sum value is greater than we will replace the min value.

Now if the flag is set then we add element value in the sum and would compare first that it is greater than 0 and if it is then we would compare with min value and if it is also greater than it, we will replace the min value with sum. If the sum value is not greater than 0 then we will reset the flag and also set sum=0

``````public class Solution {
public int[] maxset(int[] A)
{
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();
long sum = 0,max=0,sum1=0;
for(int i=0;i<A.length;i++)
{
if(A[i]<0)
{
if((sum1>sum)||(sum1==sum && b.size()>a.size()))
{
sum = sum1;
a.clear();
a.addAll(b);
}
sum1 = 0;
b.clear();
}
else
{
b.add(A[i]);
sum1 = sum1 + A[i];
}
}
if((sum1>sum)||(sum1==sum && b.size()>a.size()))
{
a.clear();
a.addAll(b);
}
int c[] = new int[a.size()];
for(int i=0;i<c.length;i++)
c[i] = a.get(i);
return c;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.