Maximum and sub-arrays/MAX and sub-sequences

Source: Internet
Author: User

The maximum and sub-arrays are the array and the largest subarray, aka the largest and the subsequence. A subarray is a contiguous n-element in an array, such as A2,A3,A4, which is a 3-length sub-array. As the name implies, the maximum and sub-arrays are required and the largest sub-array.

  

An array of n elements contains n sub-arrays of length 1: {A0},{A1}, ... {an-1};

An array of n elements contains n-1 with a length of 2: {a0,a1},{a1,a2},{an-2,an-1};

............................................................................................................

An array of n elements contains 1 sub-arrays of length n: {a0,a1,..., an-1};

Therefore, an array of length n contains the number of sub-arrays n+ (n-1) +...+1=n* (n-1)/2.

The first reaction is the brute force method, which is exhaustive and the maximum value of the and and calculated values of all the sub-arrays of the array. This method is simple, intuitive and easy to think, and there are several different ideas. The first method is to find out all the sub-arrays of length 1, and then the number of sub-arrays of length 2, and so on, until all the length of the sub-array of N and, and in the process of finding a sub-array and the maximum value recorded. The pseudo code is as follows:

The Maxsum=arr[0];//maxsum records the maximum number of sub-arrays and

for (i=1,i<=n;i++) {//Sub-array length

for (j=0;j<n;j++) {//Sub-array start position (array subscript)

Sum=0;//sum records the current sub-array and

for (k=j;k<n&&k<j+i;k++) {//Sum

SUM+=ARR[K];

}

if (sum>maxsum) maxsum=sum;

}

}

The second method is to ask all the a[0 of the sub-array starting with [], and then all the sub-arrays starting with a[1], and so on, until finally find all the sub-arrays starting with a[n-1] and the maximum value recorded in the process of finding the subarray and. The pseudo code is as follows:

int maxsubarraysum (int *arr,int n) {

int i,j,maxsum=arr[0],sum;

for (i=0;i<n;i++) {//Sub-array start position

sum=0;

for (j=i;j<n;j++) {///arr[i] The sum of the different lengths of the sub-arrays, summed is a progressive process

SUM+=ARR[J];

if (sum>maxsum) maxsum=sum;

}

}

return maxsum;

}

Although the brute force method can obtain the solution of the problem, the brute force method is usually not what we want, and its time complexity is great. We want to find a more effective way to solve the problem, so we need to think about the characteristics of the problem, get the problem to start writing code is the most taboo, before the code needs to reconsider. The maximum and the subarray must end with an element in the array a[i] (0<=i<n), so we can first find out the maximal and maximum number of sub-arrays at the end of each element, the maximum of which is the maximal subarray and. The sum of the latter element needs to use the result of the previous element, the recursive formula is Maxsumend[i]=max{maxsumend[i-1]+a[i],a[i]}, the code is as follows:

int MAXSUMARRAYSUMD (int *arr,int n) {
int i,maxsum=arr[0];
int *maxsumend;
Maxsumend= (int*) malloc (sizeof (int) *n);
MAXSUMEND[0]=ARR[0];
for (i=1;i<n;i++) {
if (Maxsumend[i-1]+arr[i]>arr[i])

Maxsumend[i]=maxsumend[i-1]+arr[i];
Else

      Maxsumend[i]=arr[i];

if (maxsumend[i]>maxsum)

Maxsum=maxsumend[i];
}
Free (maxsumend);
return maxsum;
}

This topic if we can think of some mathematical knowledge can get a more concise answer, excellent program ape must have a very thick mathematical foundation and agile Mathematical thinking, but there is a very thick mathematical foundation and agile Mathematical thinking is not necessarily a procedural ape, history proves that countless excellent mathematicians have become economists, Computer This professional learning to the back also left English and math, Wall Street vampire all are math geeks.

One number plus a negative number will be smaller, a count plus 0, and a number only plus one positive and only the larger. If the maximum and sub-array is a[i],a[j],a[k], then there must be a[i]+a[j]>0, if a[i]+a[j]<=0, then the maximum and sub-array is a[k]. Therefore, we can accumulate sums from a[0], as long as the accumulated sum and greater than 0 continue to accumulate backwards, if the sum of and less than 0, then discard, from the next element from the new beginning to accumulate, and in the accumulation process to record the maximum value. The code is as follows:

int maxsubarraysum (int *arr,int n) {
int i,maxsum=arr[0],sum=0;
for (i=0;i<n;i++) {
Sum+=arr[i];
if (sum>maxsum) {//record maximum accumulation and
Maxsum=sum;
}
if (sum<0) {//accumulate and less than 0 discard
sum=0;
}
}
return maxsum;
}

You may also want to give the maximum and sub-arrays, so the maximum and the starting position of the Subarray are recorded in the process of finding the maximum and sub-arrays. The code is as follows:

int maxsubarraysumpos (int *arr,int n) {
int i,maxsum=arr[0],sum=0;
int start=0,end=0,s=0,e=0;
for (i=0;i<n;i++) {
Sum+=arr[i];
if (sum>maxsum) {
Maxsum=sum;
E=i;
Start=s;
End=e;
}
if (sum<0) {
sum=0;
s=i+1;
e=i+1;
}
}
printf ("start=%d end=%d\n", start,end);
return maxsum;
}

The road of Rome is not smooth, heroes do not mean their martial arts cheats, comment area for you heroes stay.

Maximum and sub-arrays/MAX and sub-sequences

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.