Maximum sub-array and (ring array)

Source: Internet
Author: User

I. Title:

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

Two. 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).

Three. Members

The main by Chongdong and Yuayan a common knot pair development completed.

Chongdong is mainly responsible for program analysis, code programming.

Yuayan is responsible for code review and Code test planning.

Four. Design ideas

First of all, our first idea is to convert a ring array into a one-dimensional array, and then, based on an array of solutions to the problem, but we have encountered a problem, our one-dimensional array is a complete traversal, can not control the number of traversal, so this idea is good, but can not be achieved.

Later, we consider that an n-dimensional ring array can be converted to n the largest subarray, and then the maximum number of sub-subarray of the largest number of words, and then compare the size of these to find the maximum, then a ring array of the maximum value of the sub-array.

Five. Source code

#include <iostream.h>
#include <stdlib.h>
int maxsum (int *a,int n)//one-dimensional array for maximum
{
int Tempstart =0,sum=0,max=-1000;
int i;
int start,end;
Start=end=0;
for (i=0;i<n;i++)
{

if (sum<0)
{
Sum=a[i];
tempstart=i;
}
Else
{
Sum+=a[i];

}
if (Sum>max)
{
Max=sum;
Start=tempstart;
End=i;
}


}
cout<< "start=" <<start<<endl<< "end=" <<i<<endl;
return Max;
}
int main ()
{
int b[10000];
int max[10000];
int num,i;

cout<< "Number of input array members:";
cin>>num;
for (i=0;i<num;i++)
{
B[i]= (rand ()%1000)-500;
}
cout<< "Output array:" <<endl;
for (i=0;i<num;i++)
{
cout<<b[i]<< "\ t";
}
cout<<endl;
//-----------------------------
For (i=0;i< (num-1); i++)//Convert a ring array to a one-dimensional array
{
B[num+i]=b[i];
}


int d[1000],st,en;

for (i=0;i<num;i++)//Convert one-dimensional array to n maximum sub-array
{

for (int j=0;j<num;j++)
{
D[J]=B[I+J];
Max[i]=maxsum (D,num);

}


}

int maxm=max[0];//The maximum of the maximum subarray of n maximum sub-arrays
for (i=0;i<num;i++)
{

if (Maxm>max[i])
{
Maxm=max[i];
}
}

//---------------------------
cout<< "Maximum sub-array and as:" <<maxm<<endl;

return 0;

Six. Results
}

Seven. Design Summary

Sometimes design ideas are simple, but the implementation of the way difficult, so according to their ability to use their own can be used but not necessarily simple Russian design ideas can sometimes be faster to complete the requirements of the topic.

This knot to let me feel quite deep, just at the beginning there are ideas, but the implementation of a lot of time, the original ideas can not be more.

Pair development Although requires the cooperation of two people, but this also requires two people have the ability to complement each other, otherwise open in a place, often outweigh the gains.

Maximum sub-array and (ring 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.