# I have algorithm problem

• November 11th, 2009, 03:21 PM
Newoor
I have algorithm problem
Here is the problem:

Given a list of integers, A0, A2, ... , An-1, each of which may be

positive or negative, find the sublist of integers Ai, ..., Aj which has

the maximum sum of any sublist. If all the integers are negative,

return 0.

A sublist has to start at some position in the original list, finish at some later position and

include every number which is in the original list between those positions. So, for

example if the list is {10,-20,11,-4,13,3,-5,-17,2,15,1,-7,8} then the answer

is 23 since this is the sum of the list {11,-4,13,3} and no other sublist has a higher

sum. The answer is always at least 0, because the empty list is always a possible sublist,

and its sum is 0.

What I have done is

Code :

```import java.util.*;   class Exercise6c { // Front end code for the problem set as Exercise 6   public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter some numbers (all on one line, separated by commas):"); String line = input.nextLine(); String[] numbers = line.split(","); int[] array = new int[numbers.length]; for(int i=0; i<array.length; i++) array[i]=Integer.parseInt(numbers[i].trim()); int highSum = highestSum(array); System.out.println("The highest sum of a sublist of the numbers is: "+highSum); }   public static int highestSum(int[] a) { int sum = 0; int sm = 0; for(int i = 0; i < a.length; i++){ for(int j = 1; j < a.length; j++){ // sum = a[i]; for (int k = 0; k < (j-i)+1 ; k++){ sum = sum + a[i+k]; if (sum > sm ){ sm = sum ; } } }   } return sm ; // ... To be filled in ... }   }```

just look at method highestSum.( Apprently one of the applications is stock market analysis.)
Can anyone solve the problem of just altering the method highestSum, to find the solution to the task.
• November 11th, 2009, 05:02 PM
rsala004
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; }   }```
• November 11th, 2009, 05:47 PM
copeg
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;```
• November 11th, 2009, 07:11 PM
rsala004
Re: I have algorithm problem