public static int maxSubSum2(int [] a)
{
int maxSum = 0;
for(int i =0; i < a.length; i++)
{
int thisSum = 0;
for(int j=i; j < a.length; j++)
{
thisSum += a[j];
if(thisSum > maxSum)
maxSum = thisSum;
}
}
return maxSum;
}
Lets say we have this array: 8,-4,3,2,-5,-6,10
So, on the first iteration, i=0; j=0; maxSum = 8, thisSum = 8.
2nd it: i=1 j=1; maxSum = 8, thisSum = 4
3rd it: i = 2; j = 2; maxSum = 8, thisSum =7
4th it: i=3,j=3, maxSum =9, thisSum = 9
5th it: i=4,j=4, maxSum =9, thiSum =4
6th it: i=5,j=5, maxSum =9 thisSum =-1
7th it: i=6, j=6, maxSum =9 thisSum = 8
Now, i ran this program nd the answer i got was 10, not 9. According to my understanding, I should be getting
9. We are finding maximum subsequence sum.
I will appreciate if you anyone will explain to me interms of iterations.

New Topic/Question
Reply



MultiQuote









|