Returns the sum of the largest sub-arrays in an array of concatenated integers

Source: Internet
Author: User

First, title:N Returns the and of the largest subarray in an integer array.

Second, requirements:

(1) n Enter an array of shapes with positive and negative numbers in the array. (2) a contiguous integer or integers in an n array make up a sub-array, each of which has a and. (3) n if array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest. (4) n returns the position of the largest sub-array at the same time. (5) The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n). third, the problem-solving ideas:because the title requires the array to be adjacent, allow output A[I-1] ... A[n-1],a[0] ... A[J-1] is the sum of the largest, so define an n array, (1) from the second integer start to nth integer to move forward one bit, the original first integer is put to the last one, the formation of a new array, (2) the largest array of novelty arrays and then repeat (1) until the Loop n times, The last array is equal to the initial array. The method for finding the maximum subarray is: from the first number (judging the first number of positive or negative, if negative, to see the next number, if it is continuing) start the array (no plus a number, we have to judge the number of positive and negative, if minus, the previous value as the maximum value of the storage, and then continue, if positive, continue) add, each plus a number of pairs and If less than 0, discard the current value, if greater than 0, the next number compared with 0, less than zero, first the maximum value with the previous and compared, in the Let and plus this number, if greater than 0, directly let and plus this number, and then take the maximum and the maximum value between and. (The requirement for an O (n) for the time complexity of the completion). Iv. Source code:

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intN=20; intN=-20; intM=20; inta[]=New int[N];   Input (A,N,N,M); //randomly generated a[n]System.out.println ("Array a[n] is:");  for(inti=0;i<n;i++)//output array A[n]{System.out.print (A[i]+" ");                } System.out.println (); inth[]={a[0],0,0};  for(inti=1;i<=n;i++)//To find the largest subarray and{Array (a,n); Maxarray (H,3, A,n,i); } System.out.println ("Maximum sub-array is:"); if(h[1]>h[2])        {             for(inti=h[1];i<n;i++) {System.out.print ("a[" +i+ "]"); }             for(inti=0;i<=h[2];i++) {System.out.print ("a[" +i+ "]"); }        }        Else if(h[1]==h[2]) {System.out.print ("a[" +h[1]+ "]"); }        Else        {             for(inti=h[1];i<=h[2];i++) {System.out.print ("a[" +i+ "]");        }} System.out.println (); System.out.println ("Maximum sub-array and as:" +h[0]); }        Static voidInput (intA[],intNintNintM//randomly generated array a[n]    {         for(inti=0;i<n;i++) {A[i]=(int) (N+math.random () * (m-n+1)); }    }        Static voidArrayintA[],intN//Put the first number of the array to the last, rearrange the array    {        intQ=a[0];  for(inti=0;i<n-1;i++) {A[i]=a[i+1]; } a[n-1]=Q; }        Static voidMaxarray (intH[],intLintA[],intNintM//Sets the maximum subarray for an array and records its maximum subarray, and the starting position of the array    {        intMax=a[0]; intA=a[0]; intB=0; intC=0; intD=0;  for(inti=1;i<n;i++)        {            if(a<0) {A=A[i]; if(max<A) {Max=A; b=i; C=i; D=b; }                Else{b=i; }            }            Else            {                if(a[i]<0)                {                    if(max<A) {Max=A; C=i-1; D=b; } A=a+A[i]; }                Else{A=a+A[i]; if(max<A) {Max=A; C=i; D=b; }                }            }        }        if(h[0]<=Max) {h[0]=Max; if((D+m) <N) {h[1]=d+m; }            Else{h[1]=d+m-N; }            if((C+m) <N) {h[2]=c+m; }            Else{h[2]=c+m-N; }        }    }}

V. Operation of the program:

Vi. Summary:

I am responsible for programming, strict Avon responsible for testing.

The greatest experience of the program is: no good programming ideas, take the problem can not be as soon as possible to solve the problem, no better thinking, no completion requirements. For their own ideas can not be very good expression, I do not know if this could be exercised in the future. Also feel that their program is more complex than others, the number of lines of code too much.

Returns the sum of the largest sub-arrays in an array of concatenated integers

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.