Subsequence and maximum Problems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.