Quadratic Maximum contiguous subsequence sum algorithm
int maxSubSum2( const vector<int> & a)
{
int maxSum = 0;
for (int i = 0; i< a.size(); ++i)
{
int thisSum = 0;
for (int j = i; j < a.size(); ++j)
{
thisSum += a[j];
if (thisSum > maxSum)
maxSum = thisSum;
}
}
return maxSum;
}
I was wondering if anyone can explain how the algorithm works? I am good with for loops, I'm just bad at nested ones. Is "thisSum" always 0 every time the outer for loop on line 8 runs or is it static?
Thank you very much! I am trying really hard to understand the algorithm. Please help me! I really appreciate the time and effort.