Re: I have algorithm problem

pretty clean ;]

works

wow im getting good at this ;] only took 5 minutes

Code :

public class sublist
{
int arr[] = {10,-20,11,-4,13,3,-5,-17,2,15,1,-7,8};
public int highestSum(int[] a)
{
int max=0;
int cycle;
if(allNeg(a))
return 0;
//Shifts starting index by 1 every time the Ending index reaches its max (also shifts Ending index back to Starting index)
for(int i =0; i< a.length; ++i) //defines Starting index
{
for(int j=i; j< a.length;++j) //defines Ending index
{
cycle=0;
for(int k=i; k <=j ; ++k) //Sums from Starting index to Ending Index
{
cycle+=a[k];
}
if(cycle > max)
max = cycle;
}
}
return max;
}
private boolean allNeg(int[] a)
{
for(int i=0; i<a.length; ++i)
if(a[i] >= 0)
return false;
return true;
}
}

Re: I have algorithm problem

The k loop does the job, but isn't necessary...

Code :

//check for all negative...the check for the max series
int sum = 0;
for ( int i = 0, st = 0; i < a.length; i++, st = 0 ){
for ( int j = i; j < a.length; j++ ){
st += a[j];
if ( st > sum ){ sum = st;}
}
}
return sum;

Re: I have algorithm problem