Enter a set of integers to find the maximum value of the subarray. (after the array is end-to-end)

Source: Internet
Author: User

Enter a set of integers to find the maximum value of the subarray.

Topics : returns the and of the largest subarray in an array of one-dimensional integers.


Enter a one-dimensional shape array with positive and negative numbers in the array.

One-dimensional arrays end-to-end, like one end-to-end tape.

One or more consecutive integers in an array make up a sub-array, each of which has a and.

The maximum value for the and of all sub-arrays.


10-9 8 7-5 3


I : 4 3 2) 1 0


Nall:3 3 7 15 15 16


Nstart:3-2 7 15 6 16


Nstart = Max (Arr[i], arr[i]+nstart);

Nall = Max (Nall, Nstart);

Nstart To record the result after each and the previous number, to the original Nall do the comparison, and Nall represents the maximum value of the Subarray and the array.


when it comes to the end, it may appear like 1,2 -3,4 This should be the case 4+1+2

Array was originally the , -3,4 Now let each number do an array header once, turning into a new array, for example 2 , -3,4,1 also have -3,4,1,2 and the 4,1,2 , -3 these three arrays, that is, the array is rotated once, the new three array will still be the maximum value of the sub-array, and then compared with the original array of sub-array and maximum value, come out the last sub-array and maximum value.


  1. Source code: /* To find the maximum value of the number of arrays in a neutron array. Cloud fly at the end of 2016.4.11 * *
  2. #include <iostream>
  3. using namespace std;
  4. int max (int x, int y)
  5. {
  6. ???? return (x > Y)? x:y;
  7. }
  8. To find the maximum value of a neutron array of an array
  9. int maxsum2_v (int arr[], int N)
  10. {
  11. ???? int i;
  12. ???? int Nall, nstart; //nall The maximum number of sub-arrays and
  13. ???? Nall = arr[n-1];
  14. ???? Nstart = arr[n-1];
  15. ???? for (i = n-2; I >= 0; i--)
  16. ???? {
  17. ?????????? Nstart = Max (Arr[i], arr[i]+nstart); a large number is retained after each sum of the previous numbers of the array.
  18. ?????????? Nall = Max (Nall, Nstart); //After keeping a larger number above, the original maximum and current and comparison, take a larger value
  19. ????}
  20. ???? return Nall;
  21. }
  22. void Main ()
  23. {
  24. ??? //n integer number, arr "" Array, anotherarr[100] for the computed rotation array, max Word Group and maximum value
  25. ??? int n=100,arr[100],anotherarr[100],max1=-100,max=-100;
  26. ??? cout<< " Please enter an integer number:";
  27. ??? cin>>n;
  28. ??? cout<< " Please enter an integer, each number is separated by a space:" <<endl;
  29. ??? for (int i=0;i<n;i++)
  30. ??? {
  31. ?????? cin>>arr[i];
  32. ?????? Anotherarr[i]=arr[i];
  33. ???}
  34. ??? Max=maxsum2_v (Arr,n); //Call the Maxsum2_v function
  35. ??? for (int i=1;i<n;i++) //Rotate the number in the array one at a time to create a new additional array
  36. ??? {
  37. ?????? for (int j=0;j<n;j++)
  38. ?????? {
  39. ????????? //Start rotation, an array of arr is required after the equals sign, and if used later, array confusion occurs after multiple loops
  40. ????????? anotherarr[j]=arr[(J+i)%n];
  41. ????????? Max1=maxsum2_v (Anotherarr,n); //Call the Maxsum2_v function
  42. ??????}
  43. ?????? if (Max1>max)
  44. ????????? MAX=MAX1;
  45. ???}
  46. ??? cout<< " sub-array and maximum:" <<Max<<endl;
  47. }


Summary :

This time the sub-array and the practice, the original array end-to-end into a chain, such as 1,2-3,4 This array, will appear 4,1,2 three sub-arrays and the largest, rather than from the back of the sub-array and the largest, so need to rotate the array inside, so that each value is rotated once the array header, Then we get another array, call the Subarray and the maximum value of the method to find the sub-array and the maximum value of each new array, and then compare, to obtain the maximum value of the sub-array.

You can not simply think of 1,2,-3,4 as 1,2,-3,4,1,2,-3 this array, and if the array is positive, the maximum value of the error will be increased.

Enter a set of integers to find the maximum value of the subarray. (after the array is end-to-end)

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.