Question: Write a program The maximum contiguous subsequence sum algorithms in the text do not give any indication of the actual sequence. Modify them so that
Write a program The maximum contiguous subsequence sum algorithms in the text do not give any indication of the actual sequence. Modify them so that they return in a single object the value of the maximum subsequence and the indices of the actual sequence. Data Structure and Algorithm Analysis in Java 2.19 /** * Quadratic maximum contiguous subsequence sum algorithm. */ public static int maxSubSum2( int [ ] a ) { int maxSum = 0; for( int i = 0; i < a.length; i++ ) { int thisSum = 0; for( int j = i; j < a.length; j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) maxSum = thisSum; } } return maxSum; } /** * Linear-time maximum contiguous subsequence sum algorithm. */ public static int maxSubSum4( int [ ] a ) { int maxSum = 0, thisSum = 0; for( int j = 0; j < a.length; j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) maxSum = thisSum; else if( thisSum < 0 ) thisSum = 0; } return maxSum; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
