Question: This algorithm takes in a sequence of positiveegative integers and returns the maximum possible sum of a contiguous subsequence Ai....Aj. Please revise the algorithm so

This algorithm takes in a sequence of positiveegative integers and returns the maximum possible sum of a contiguous subsequence Ai....Aj. Please revise the algorithm so that it also returns the indices of Ai and Aj.

This algorithm takes in a sequence of positiveegative integers and returns the

// Recursive maximum contiguous sum algorithm int maxSubSum3 (const vector int> & a) return maxSumRec(a, 0, a.size() - 1); // Used by driver fucntion above, this one is called recursively int maxSumRec( const vector& a, int left, int right) // base case if (left == right) if (a[left] > 0) return a[left]; else return 0; int center = (left + right) / 2; int maxLeft Sum = maxSumRec(a, left, center); int maxRight Sum = maxSumRec(a, center+1, right); // recursive call // recursive call int maxLeftBorderSum = 0, leftBorderSum = 0; for (int i = center; i >= left; --i) leftBorderSum += a[i]; if (leftBorderSum > maxLeftBorderSum) maxLeftBorderSum = leftBorderSum; int maxRightBorderSum = 0, rightBorderSum = 0; for (int j = center+1; j maxRightBorder Sum) maxRightBorderSum = rightBorderSum; return max3 (maxLeft Sum, maxRight Sum, maxLeftBorderSum + maxRightBorderSum); int max3(int x, int y, int z) int max = x; if (y > max) if (z > max) return max; max = y; max = z; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!