Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

// 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books