Pairs (the largest subarray of arrays and)

Source: Internet
Author: User

Topic:

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

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.

(I am mainly responsible for program analysis, code programming; Zhang is responsible for code review, Code test plan.) )

Ideas:

The difference from the first is that it consists of the end-to-end connection, forming a ring. Thinking about it, we constructed three arrays: (A) arr[] is the original array, (B) max[] holds the maximum value of all the sub-arrays with each element as the head, and (C) arrcopy[] A new one-dimensional array that constructs the original array. Their relationship is as follows:

MAX[0] is the maximum value in {arr[0], arr[0]+arr[1] 、...、 arr[0]+arr[1]+...+arr[n-1]+arr[n]};

MAX[1] is the maximum value in {arr[1], arr[1]+arr[2] 、...、 arr[1]+arr[2]+...+arr[n]+arr[1]};

......

MAX[N-1] is the maximum value in {arr[n-1], Arr[n-1]+arr[n] 、...、 arr[n-1]+arr[n]+...+arr[n-3]+arr[n-2]};

Max[n] is the maximum value in {Arr[n], arr[n]+arr[1] 、...、 arr[n]+arr[1]+...+arr[n-2]+arr[n-1]}. Then compare the max array, and find the maximum value from it.

Arrcopy[] is an array of {arr[1], arr[2] 、...、 arr[n], arr[1] 、...、 arr[n-1]}, and its role is to mark the location of the subarray.

<span style= "Font-family:microsoft Yahei; font-size:16px; " > #include <iostream.h>

#include <stdlib.h>

#define AMOUNT 100

int main ()

{

int Arr[amount];

int Arrcopy[amount];

int Max[amount];

int i,j;

int n;

int head,rear; Start of sub-array, end

int mount,amount;

cout<< "input mount:";

cin>>mount;

while (mount<=0)

{

cout<< "Illegal input! Again: ";

cin>>mount;

}

amount=2*mount-1;

for (i=0;i<mount;i++)

{

Try

{

N=rand ()%2;

if (n==0)

{

Arr[i]=rand ()%100;

Arrcopy[i]=arr[i];

Max[i]=arr[i];

}

Else

{

Arr[i]=-rand ()%100;

Arrcopy[i]=arr[i];

Max[i]=arr[i];

}

}

catch (long int e)

{

cout<< "Long Inter=ger exception!" <<endl;

}

}

for (i=0;i<mount;i++)

{

cout<<arr[i]<< "";

if ((i+1)%10==0)

{

cout<<endl;

}

}

for (i=mount;i<amount;i++)

{

Arr[i]=arr[i-mount];

Arrcopy[i]=arr[i];

}

for (j=0;j<mount;j++)

{

for (i=j+1;i<j+mount;i++)

{

Arr[j]=arr[j]+arr[i];

if (Max[j]<arr[j])

{

MAX[J]=ARR[J];

rear=i+1;

if (Rear>mount)

{

Rear=rear-mount;

}

}

}

}

for (i=0;i<mount;i++)

{

if (Max[0]<max[i])

{

Max[0]=max[i];

head=i+1;

}

}

cout<<endl;

cout<< "from" <

cout<< "to" <<rear<< "number" <<arrCopy[rear-1]<< "End" <<endl;

cout<< "sub-arrays and the maximum values are:" <<max[0]<<endl;

return 0;

}

</span>

Operation Result:

(1) First, because there is no careful consideration, the input array length when prompted to lose a negative number, resulting in the program can not be executed normally, after modification, can be wrong to the data type to send an error, and re-enter.

(2) As you can see, the defined array length is 5, and the largest sub-array is composed of the 5th number looping to the 1th number, but it shows the 6th number in the arrcopy[] array, the position is incorrect.

(3) After modification, the end position of the ring can be found correctly.

(4) But we manually entered the number is very small, so the last change: only the input array length (test is 10), the value is randomly generated.

Summarize:

See the topic is the first thought of the data structure of the linked list structure, but soon put this idea to give up, feel with the list of trouble, listening to the class of three students of the idea, everyone's point of view is actually the array to form a ring structure, or a new one-dimensional array, and then the first to seek the largest and the same. The beginning of the puzzled is how to determine the starting and ending elements of the sub-array, subscript record is very important, because in our algorithm, the use of recursion will continue to overwrite the value of the original array, resulting in all sub-arrays after the completion and after the original array value changed, and cannot return the maximum sub-array position, so, The way to do this is to copy the array once, and then use it to locate it. Finally, the problem of data testing, manual input has a lot of subjective awareness, so the use of random number test comparison can find problems.

Feeling can solve some problems now, but the method is still like beginners, a lot of similar time, space complexity of those are not considered.

Pairs (the largest subarray of arrays 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.