Page 1 of 1

Can you please explain what the code does? public class Solution1 { /** * Cubic maximum contiguous subse

Posted: Fri May 20, 2022 10:20 am
by answerhappygod
Can you please explain what the code does?
public class Solution1 {
/**
* Cubic 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++ )
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;
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;


}

}