Maximum sub-array problem

Source: Internet
Author: User

Small slag today to update a wave, welcome to see the master of the Spray (criticism)

What is the largest subarray: In short, it is a continuous subsequence of the array a[n] from I to J (I,j are all within the (0,n) interval), and there is no other pair (A, b) to make the a[a]+a[a+1]+ ... A[B]>A[I]+A[I+1]+...+A[J]

Visible: ① only if the array contains negative elements, this problem has the value of discussion, otherwise the entire array itself is the largest subarray;

② and, I<=j (maximum sub-array may contain only one element)

③ maximum sub-array may not be unique

Idea: According to the dichotomy, the maximum number of sub-arrays at each end is nothing more than three cases:

In the left half, in the right half, contains the midpoint position element

Find out three cases of the neutron array and the largest one as a result, only the first two cases can be recursive OH

# include <iostream.h># include ". \sort\io_tools.cpp "/*subarray encapsulates the maximum number of sub-arrays obtained in a single lookup and the sum of the elements of the subarray */struct subarray{int l,r,sum;}; Subarray find_max_cross_subarray (int a[],int l,int R);//Find the largest sub-array containing the midpoint position of the sub-array in array A from L to r in the index interval subarray find_max_subarray (  int a[],int l,int R);//recursively finds the largest sub-array in array a from L to R index interval (takes the sum of three cases of the neutron array element as the maximum return value) void Main () {/* Initialization test Object */int a[6];  Inputa (a,6);  Outputa (a,6);  /* Get maximum sub-array */Subarray Sub=find_max_subarray (a,0,5); /* Output */cout<< "Result:" <<Sub.l<< "" <<Sub.r<< "" &LT;&LT;SUB.SUM&LT;&LT;ENDL;} Subarray find_max_subarray (int a[],int l,int r) {Subarray s1,s2,s3,s4;/* for sub-arrays that contain more than two elements, the half-calculation */if (l<r) {int mid= (L +) is removed R)/2;s1=find_max_subarray (A,L,MID);//Situation one s2=find_max_subarray (a,mid+1,r);//Situation two S3=find_max_cross_subarray (A,l,r) ;//Condition three/* Gets the maximum subarray of a in the L,r index interval */s4=s1.sum>=s2.sum?s1:s2;s4=s4.sum>=s3.sum?s4:s3;/* a debug message for an interval containing two (or more) elements, Can check their own ideas and code writing there is no literature and art, and deepen the understanding of algorithmic thinking */cout<< "analyse-:" <<s3.l<< "<<s3.r<<" "<<s3. Sum<<endl;} /* contains only one element: Split to the end, that is, to reach the recursive boundary */else{s4.l=s4.r=l;s4.sum=a[l];} /* Output Results of this lookup */cout<< "analyse:" <<s4.l<< "" <<s4.r<< "" <<s4.sum<<endl; return S4;} /* Because a sub-array that spans two parts is obviously not a recursive solution, it is natural to pull it out alone: the largest subarray from the midpoint to the left and the right sub-array from the middle, which is the largest sub-array spanning two halves */subarray find_max_cross_subarray (int a[],int L,int r) {int Left_sum,right_sum,max_left,max_right,sum,mid;subarray s;left_sum=right_sum=-999;//as-negative infinity bullish, No smaller than the midpoint element, so this "negative infinity is not passed to the result" sum=0;mid= (r+l)/2;for (int i=mid;i>=l;i--) {///from the left half of the sum=sum+a[i];if (sum> left_sum) {left_sum=sum;//Update the record max_left=i only if and increased;}} sum=0;/* Analysis Method Ibid. */for (int j=mid+1;j<=r;j++) {sum+=a[j];if (sum>right_sum) {right_sum=sum;max_right=j;}} /* Merge results */s.l=max_left;s.r=max_right;s.sum=left_sum+right_sum;return s;}

The actual application of the problem:

An example of the introduction of algorithms in the Divide and conquer strategy, let's see for ourselves.

Maximum sub-array problem

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: 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.