As array of two largest and

Source: Internet
Author: User

public class Arrays {
int Maxsubarray (int[] A, int n)
{
int Maxsum =a[0];
int currsum = 0;
for (int i = 0; i < n; i++)
{
for (int j = i; J < N; j + +)
{
for (int k = i; k <= J; k++)
{
Currsum + = A[k];
}
if (Currsum > Maxsum)
Maxsum = Currsum;

currsum = 0; Remember to clear the zeros here, otherwise the sum of all the sub-arrays is finally stored.
}
}
return maxsum;
}
}

public class Arraystest {

Private Arrays Array;

@Before
public void SetUp () throws Exception {
Array = new Test2 ();
}

@Test
public void Maxsubarray () throws Exception {
int []array1={-1,2,9,-4};
Assertequals (11d,array.maxsubarray (Array1,array1.length), 4);

int []array2={-1,2,3,3,-4};
Assertequals (8d,array.maxsubarray (Array2,array2.length), 5);

int []array3={-1,20,10,30,-4};
Assertequals (60d,array.maxsubarray (Array3,array3.length), 5);

int []array={-2,-3,-5,-1,-9};
Assertequals ( -1d,array.maxsubarray (Array,array.length), 5);
}
}

As array of two largest and

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.