Task 4.3 asks the largest subarray of the ring array and

Source: Internet
Author: User

Task requirements: Enter an array of shapes, with positive numbers in the array and negative values.

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)

1. Design idea: (assuming that the array length is n.) In the task request, the array can be adjacent to the end and end, so that when the array maximum subarray and the time to consider two cases: first, the largest sub-array and the array in a[0] ... A[n], excluding end-to-end connections, and the largest array and array in A[i-1] ... A[n-1],a[0] ... A[j-1] should be considered between the a[n-1],a[0] to connect together.

There will be several different ideas to solve the problem: (1) in the case of a subarray and when the array automatically loop two times, in the second to a[n-1] stop, can control the length of the sub-array is less than n, then use the loop in the previous program to find the largest array in the ring array and;

(2) The array is automatically extended one time, ({a1,a2,a3....an} is extended to {a1,a2,a3...an,a1,a2,a3,... an}), the largest array of the array of novelty, and also the length of the obtained sub-array is controlled below N;

(3) The implementation of the two methods is similar, but when implementing the position of the returned Subarray, there is no implementation because the element position of the array changes. Then changed the idea of thinking with a way of thinking about the problem: the two possible cases mentioned before the maximum number of sub-arrays and the first one directly with a loop can be obtained and the Warrior can record the first and last position of the group array; the second case is to find the smallest sub-arrays and minsum in the group. Using the entire array and subtracting it to find the maximum subarray and, at the same time, record the top and bottom positions of the minimum array and the array, and then get the maximum number of sub-arrays and the position of the arrays.

2. Source code:

//Max and (after test) of contiguous Subarray in array//Liu Zixiang 2015/4/12#include <iostream>#include<time.h>using namespacestd;voidMain () {intI, length, max, sum =0; intHead =0; intTail =0; intHead1 =0; intTail1 =0; intarr[ -];    Srand ((unsigned) time (NULL)); cout<<"Please enter the length of the array:"; CIN>>length;  for(i =0; i < length; i++) {Arr[i]= rand ()% --Ten; C7out<< Arr[i] <<"  "; }//output a randomly generated arraycout <<Endl; intMAX1 = arr[0]; intMin = arr[0];  for(i =0; i < length; i++)    {        if(Sum <=0) {sum=Arr[i]; Head=i; }        Else{sum= Sum +Arr[i]; }        if(Sum >max1) {Max1=sum; Tail=i; }} cout<<"The maximum number of sub-arrays and parts that do not include the closing touches are:"<< Max1 <<Endl;  for(i =0; i < length; i++)    {        if(Sum >=0) {sum=Arr[i]; Head1=i; }        Else{sum= Sum +Arr[i]; }        if(Sum <min) {min=sum; Tail1=i; }    }    intSUM1 =0;  for(i =0; i < length; i++) {sum1= Sum1 +Arr[i]; }    intMAX2 = sum1-min; cout<<"The maximum number of sub-arrays and the part that includes the closing touches is:"<< Max2 <<Endl; cout<<Endl; if(Max1 >=max2) {cout<<"the maximum number of sub-arrays that can be combined in two cases is:"; Max=Max1; cout<<"arr["<< Head +1<<"]--arr["<< Tail +1<<"]"<<Endl;  for(intj = Head; J <= Tail; J + +) {cout<< Arr[j] <<" "; }    }    Else{Max=Max2; cout<<"the maximum number of sub-arrays that can be combined in two cases is:" ; cout<<"arr["<< Tail1 +2<<"]--arr["<< Head1 <<"]"<<Endl;  for(intj = Tail1 +1; J < length; J + +) {cout<< Arr[j] <<"  "; }         for(intj =0; J < Head1; J + +) {cout<< Arr[j] <<"  "; }} cout<<"the maximum number of sub-arrays and is:"<<Max;} 

3.:

4. Summary: (1) When a project to achieve the function at the same time, to take the appropriate trade-offs, a function can not be achieved, you may change the implementation of other parts to achieve this function;

(2) in the realization process to establish a perspective from the opposite point of view of the problem of thinking that is reverse thinking, this topic in the second kind of and, by seeking the minimum and indirect to get the maximum and, on the ingenious reduction of a large computational volume, is a good way.

Task 4.3 asks the largest subarray of the ring array 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.