Returns the and of the largest subarray in an integer array (array-to-tail connections)

Source: Internet
Author: User

1. Title.

Title: 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. Requires a time complexity of O (n).

2. Design ideas.

The randomly generated number is continuously deposited in the array two times, and in the first, each of its sub-arrays is calculated and compared. The maximum maximum number of sub-arrays is calculated.

3. Code.

#include <iostream>using namespacestd; #include<ctime>#include<cstdlib>#defineMAX 10000intMain () {intk,a[max],b[max],m,n=0, P,c,d; cout<<"Please enter the number of arrays:"<<Endl; CIN>>K; Srand (Time (0)); cout<<Endl; cout<<"the numbers in the array are:"<<Endl;  for(intI=0; i<k;i++) {A[i+k]=a[i]=rand ()% -- -; cout<<a[i]<<" "; } cout<<Endl; cout<<Endl;  for(i=0; i<k;i++) {m=A[i]; B[n]=m; N++;  for(intj=i+1; j<i+k;j++) {m=m+A[j]; B[n]=m; N++; }    }    intmax=b[0];  for(i=1; i<n;i++)    {        if(max<B[i]) {Max=B[i]; P=i+1; }    }    //p=p+1;cout<<"the maximum number of sub-arrays is:"; cout<<max<<Endl; cout<<Endl; C=p/K; D=p%K; cout<<"the maximum number of sub-arrays is:";  for(i=c;i<c+d;i++) cout<<a[i]<<" "; cout<<Endl; return 0;}

4. Run the results.

5. Experience.

Pair development can speed up the programming speed, and complete the experiment programming faster and better.

6. Photos.

Returns the and of the largest subarray in an integer array (array-to-tail connections)

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.