Returns the maximum continuous subsequence sum in the array. For example, given an array A = {4,-3, 5,-2,-1, 2, 6,-2}, the maximum subsequence is 11, that is, 11 = 4 + (-3) + 5 + (-2) + (-1) + 2 + 6.
The Java implementation code is as follows:
Public classMaxsubseque {
Public static voidMain (string [] ARGs ){
Int[] A = {4,-3, 5,-2,-1, 2, 6,-2 };
IntMaxsum = 0;
For(IntI = 0; I <8; I ++ ){
// System.Out. Println ("I =" + I + ");
For(IntJ = I; j <8; j ++ ){
// System.Out. Println ("J =" + J + "");
IntThissum = 0;
For(IntK = I; k <= J; k ++ ){
// System.Out. Println ("k =" + K + ");
Thissum + = A [k]; // this step is a basic operation. number of executions: 8*8*8
}
If(Thissum> maxsum) {// this step is a basic operation. number of executions: 8*8*8
Maxsum = thissum; // system.Out. Println ("Update maxsum =" + maxsum );
}Else{
// System.Out. Println ("at this time thissum =" + thissum + ", so maxsum is still:" + maxsum );
}
}
}
// System.Out. Println ("The finalmaxsum is:" + maxsum );
}
}
Therefore, the time complexity of this algorithm is T (n) = O (N ^ 3)
Subsequence and maximum Problems