Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Revise the algorithm given for finding the maximum subsequence sum so that it finds the indices (i,j) of the subsequence as well. /** * Recursive

Revise the algorithm given for finding the maximum subsequence sum so that it finds the indices (i,j) of the subsequence as well.

image text in transcribed

/** * Recursive maximum contiguous subsequence sum algorithm. * Finds maximum sum in subarray spanning a[left..right]. 3 * Does not attempt to maintain actual best sequence. */ int maxSumRec( const vector & a, int left, int right ) { if( left == right ) // Base case 4 if( a[ left ] > 0 ) return a[ left ]; 9. 10 else 11 12 return 0; 13 int center = ( left + right ) / 2; 14 = maxSumRec ( a, left, center ); int maxRightSum = maxSumRec( a, center + 1, right ); 15 int maxLeftSum 16 17 int maxLeftBorderSum = 0, leftBorderSum = 0; 18 for( int i = center; i >= left; --i ) { leftBorderSum += a[ i ]; 19 20 21 if( leftBorderSum > maxLeftBorderSum ) 22 maxLeftBorderSum = leftBorderSum; 23 } 24 25 26 int maxRightBorderSum = 0, rightBorderSum = 0; for( int j = center + 1; j maxRightBorderSum ) 27 28 29 30 31 maxRightBorderSum = rightBorderSum; } 32 33 return max3( maxLeftSum, maxRightSum, 34 maxLeftBorderSum + maxRightBorderSum ); 35 } 36 37 /** 38 * Driver for divide-and-conquer maximum contiguous 39 40 subsequence sum algorithm. */ int maxSubSum3( const vector & a ) 41 42 { 43 return maxSumRec( a, 0, a.size( ) - 1 ); 44 } 45

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

More Books

Students also viewed these Databases questions

Question

I would like to delete my wuestion under review

Answered: 1 week ago

Question

=+j Describe an effective crisis management program.

Answered: 1 week ago