# Connected maximum subarray and (pair development)

Source: Internet
Author: User

Title: Returns the and (connectivity) of the largest sub-array in a two-dimensional array

Design ideas: Divide the number of rows into a few one-dimensional arrays, for the one-dimensional array, the sum of their largest contiguous array, and record the maximum continuous array of the first and last position, and then determine the maximum continuous array of several one-dimensional array of the position of whether or including (for example, the first row is 1 and 4, the second row is This is connected). Finally, add a positive number that is not included (must be between the first and last position of the largest contiguous array on the previous line). The sum of the outputs before the output is OK.

Code:

`#include <iostream>using namespace std;    int Max (int n,int a[],int *smark,int *mmark) {int b={0};    int i,sum1=0,max1=0;        for (i=0;i<n;i++) {if (sum1<0) {sum1=a[i];        } else {sum1=sum1+a[i];    } b[i]=sum1;    } Max1=b;             for (i=0;i<n;i++) {if (Max1<b[i]) {max1= b[i];         *mmark = i;             }} for (i = *mmark;i >= 0;i--) {if (b[i] = = A[i]) {*smark = i;        Break }} return max1;}    void Main () {int m,n,i,j,smark,mmark,t2;    int Sum,max;    int up,down,t;    int a,b;    cout<< "Enter rows and columns of two-dimensional arrays";    cin>>m>>n;        for (i=0;i<m;i++) {for (j=0;j<n;j++) {cin>>a[i][j];        }} for (i=0;i<m;i++) {for (j=0;j<n;j++) {b[j]=a[i][j]; } Sum=max (N,b,&sMark,&mmark);                                           Up[i]=smark;        Down[i]=mmark;     T[i]=sum;    } T2=t;        for (i=0;i+1<m;i++) {if (up[i]<=down[i+1] && down[i]>=up[i+1]) {t2+=t[i+1];                   } for (j=up[i];j<up[i+1];j++) {if (a[i+1][j]>0) t2+=a[i+1][j]; Discriminant independent Positive}} cout<<t2<<endl; }`

Results:

Summary: An array to maximize the number of sub-arrays and can evolve so many requirements, we write code should be more flexible to solve, on the basis of the previous year. There are shortcomings that need to be perfected.

Connected maximum subarray and (pair development)

Related Keywords:

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.