Question: 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.

Revise the algorithm given for finding the maximum subsequence sum so that

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

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!