MaxSum algorithm with negative values in Array

How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file?

The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them. Thanks

Code Java:

case 1:
int sum;
System.out.print("Enter list of comma-delimeted integers: ");
Scanner scan = new Scanner(System.in);
String input2=scan.next();
String[] num = input2.split(",");
int[] a= new int[num.length];
for (int i = 0; i < num.length; i++) {
a[i] = Integer.parseInt(num[i]);
}
sum =maxSum(a);

Code Java:

case 2:
BufferedReader reader = null;
Scanner sc = null;
String line = "";
try {
reader = new BufferedReader(new FileReader("integers.txt"));
sc =new Scanner(new BufferedReader(new FileReader("integers.txt")));
while (sc.hasNext()) {
line = sc.next();
}
String s[] = line.split(",");
a = new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
}
finally {
if (reader != null) {
reader.close();
}
}
System.out.println(line);
sum=maxSum(a)

Code Java:

public static int maxSum(int [] a)
{
int maxSum = 0;
int this_sum=0;
int i=0;
for(int j=0; j< a.length; j++)
{
this_sum += a[j];
if(this_sum > maxSum)
{ maxSum = this_sum;
}
else if( this_sum<0)
{
i=j+1;
this_sum=0;
}
}
return maxSum;
}

Re: MaxSum algorithm with negative values in Array

lets talk about your method **public static int maxSum(int [] a)**

can you explain what is that doing? a comment maybe.

As what I understand with your problem, you want to get the sum of your integer array right? I don't get what's that **public static int maxSum(int [] a)** is all about? there is a conditional statement inside the loop, what is it checking? and why?

Re: MaxSum algorithm with negative values in Array

The maxSum is given the problem is the implementation of it but what it is is basically an algorithm that prints out the sum of the

integer array. It works as is but it won't let me input negative integers for the sum.

Definition: The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers (containing at least one positive number) which has the largest sum

Re: MaxSum algorithm with negative values in Array

so if I input 1,2,3,-4,5,-6 the result is 1? is it right?

Re: MaxSum algorithm with negative values in Array

In theory yes it should but the result of that is actually 7??? I have no clue where the numbers are coming from but

1,2,3,4,5,6 prints out 21

Re: MaxSum algorithm with negative values in Array

7 in your code? how come that it should be 7? I'm sorry, I'm a little bit confused on your requirements

Re: MaxSum algorithm with negative values in Array

No you were correct it should be 1 but its printing out 7, the code just prints out the sum you had the correct idea but when adding negatives it warps the result into a strange result

Re: MaxSum algorithm with negative values in Array

that is because of your conditional statement in **public static int maxSum(int [] a)** try to remove them and just put a max_sum += the array element. that should do it

Re: MaxSum algorithm with negative values in Array

I'm not supposed to change that algorithm, I can only change the case statements that call upon it

Re: MaxSum algorithm with negative values in Array

why you cannot change that? Actually that is the reason why you got a wrong sum. And there is also one nonsense statement **i=j+1;**, that statement is completely nonsense. you just equated **i** to **j + 1** and then ignored it.

Re: MaxSum algorithm with negative values in Array

You were right, i inputted the algorithm incorrectly...now the data file in case 2 is still posting incorrectly, heres the updated algorithm

Code :

{
public static int maxSum(int[] a, int n)
int max_sum = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int this_sum = 0;
for (int k = i; k <= j; k++) {
this_sum += a[k];
}
if (this_sum > max_sum) {
max_sum = this_sum;
}
}
}
return max_sum;
}

Then I call it

maxSum(a, a.length)

Re: MaxSum algorithm with negative values in Array

I'm still a little bit confused on what your method **maxSum** is really doing. can you explain what does that method can do?

and please explain what each statement doing. you can put a comment. because as what I understand in requirement, you just need to get the sum of the elements in the array so I don't really get the sense of making nested loop and another loop inside that nested loop and making a condition of comparing the two sum (this_sum and max_sum).