Can you please explain what the code does? public class Solution2 { /** * Quadratic maximum contiguous subseq

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 Solution2 { /** * Quadratic maximum contiguous subseq

Post by answerhappygod »

Can you please explain what the code
does?
public class Solution2 {
/**
* Quadratic maximum contiguous subsequence sum
algorithm.
* seqStart and seqEnd represent the actual best
sequence.
*/
public static int maxSubsequenceSum( 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;
int seqStart = i;
int seqEnd = j;
}
}
}

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