Can you please explain what the code does? public class Solution3 { /** * Linear maximum contiguous subsequence

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Can you please explain what the code does? public class Solution3 { /** * Linear maximum contiguous subsequence

Post by answerhappygod »

Can you please explain what the code does?
public class Solution3 {
/**
* Linear maximum contiguous subsequence sum
algorithm.
* seqStart and seqEnd represent the actual best
sequence.
*/
public static int maximumSubsequenceSum( int [
] a )
{
int maxSum = 0;
int thisSum = 0;

for( int i = 0, j = 0; j < a.length; j++
)
{
thisSum += a[ j ];

if( thisSum > maxSum )
{
maxSum = thisSum;
int seqStart = i;
int seqEnd = j;
}
else if( thisSum < 0 )
{
i = j + 1;
thisSum = 0;
}
}

return maxSum;
}
public static int[] theMCSS(int [] A) {

int maxSum
= 0;
int
seqStart = -1;
int seqEnd
= -1;
for( int i
= 0; i < A.length; i++ )
for( int j
= i; j < A.length; j++ ){
int
thisSum = 0;
for(
int k = i; k <= j; k++ )
thisSum +=
A[ k ];
if(
thisSum > maxSum ){
maxSum
= thisSum;
seqStart
= i;
seqEnd =
j;
}
}
int[]
r= {seqStart,seqEnd};
return
r;


}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply