Pair programming the sum of the largest sub-arrays in the concatenated array

Source: Internet
Author: User

1. Title:

Returns the and of the largest sub-array in an integer array.

2. Requirements:

Enter an array of shapes with positive and negative numbers in the array.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
If the 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.
Returns the position of the largest subarray at the same time.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

3. Design ideas:

Sub-case discussion of the maximum possible sub-array can occur, the first is normal, no more than the array range, the second is more complex, to use a part of the ring element, to find out the maximum of two cases, and then compare the size of the largest array of rings can be obtained and the number of sub-arrays.

4. Source code:

1#include <iostream.h>2 intMain ()3 {4     intn,k;5     intlist[100000]; 6     intmax, Min, Summax, summin;7     intTotal ;8cout<<"input Array Length:"<<Endl;9Cin>>N;Ten      while(n!=0) One     { Acin>>list[0]; -max=summax=list[0]>0? list[0]:0; -min=summin=list[0]<0? list[0]:0; thetotal=list[0]; -          for(intI=1; i<n;i++) -         { -Cin>>List[i]; +             if(summax>0) -summax+=List[i]; +             Else Asummax=List[i]; at             if(summin<0) -summin+=List[i]; -             Else -summin=List[i]; -  -Max=summax>max?Summax:max; inMin=summin<min?summin:min; -total+=List[i]; to         } +         inttmp=total-min; -K=max>tmp?max:tmp; thecout<<"the largest array of Subarray and is:"<<k<<Endl; *     } $     return 0;Panax Notoginseng}

5. Results:

6. Experience:

Through the class of students to remind and advice, our group of ideas are similar, more is to consider the complexity of the requirements, this is a more difficult problem. The problem is not afraid, afraid of is too lazy to think, do not bother to achieve and solve this problem, pair programming is good to make up for this flaw.

Pair programming the sum of the largest sub-arrays in the concatenated array

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.