[Algorithm 07] returns the largest sum of sub-arrays.

Source: Internet
Author: User

Question: enter an integer array. There are positive and negative numbers in the array. One or more consecutive integers in the array form a subarray. each subarray has a sum, calculates the maximum values of all sub-arrays and. The time complexity is O (n ).

For example, if the input array is {1,-, 10,-, 2,-5}, and the maximum sub-array is {,-, 2 }, therefore, the output is the sum of 18 in the subarray.

 

Analysis 1: Generally, the most intuitive and final way to find sub-sets that meet a certain condition in the overall set is to enumerate and find the sum of all sub-arrays, then compare and find the maximum value. However, for an array whose length is n, its sub-array has n (n-1)/2, O (n ^ 2), and then sums each array, the time complexity is O (n ). Therefore, the total time complexity is O (n ^ 3 ). Low efficiency.

Analysis 2: We know that for a sub-array, if the next number is positive, adding this positive number will increase, and adding a negative number will decrease. If the current sub-array is negative, the next number, whether positive or negative, will further reduce the sum of the sub-array. Therefore, the sub-array and the sub-array should be reset to 0. Based on this idea, we can write the following code:

1 # include <iostream>
2 # include <string>
3 using namespace std;
4
5 bool FindMaxSubarray (int * pArray, int arrayLength, int & largestSum)
6 {
7 // invalid input. false is returned.
8 if (pArray = NULL | arrayLength <= 0)
9 return false;
10
11 int curSum = 0;
12 largestSum = 0;
13 for (int I = 0; I <arrayLength; ++ I)
14 {
15 curSum + = pArray [I];
16
17 // if the current and less than 0, reset to 0
18 if (curSum <0)
19 curSum = 0;
20
21 // find the bigger sum and save it with largestSum
22 if (curSum> largestSum)
23 largestSum = curSum;
24}
25
26 // if all numbers are less than or equal to 0, find the maximum number of the array
27 if (largestSum = 0)
28 {
29 largestSum = pArray [0];
30 for (int I = 1; I <arrayLength; ++ I)
31 {
32 if (pArray [I]> largestSum)
33 largestSum = pArray [I];
34}
35}
36
37 return true;
38}
39
40 int main ()
41 {
42 cout <"Enter the length of your array:" <endl;
43 int n;
44 cin> n;
45
46 cout <"Enter the numbers in your array:" <endl;
47 int * p = new int [n];
48 for (int k = 0; k <n; ++ k)
49 {
50 cin> p [k];
51}
52
53 cout <"your array is:" <endl;
54 for (k = 0; k <n; ++ k)
55 cout <p [k] <"";
56
57 cout <endl;
58 cout <"the largest subarray sum is:" <endl;
59 int saveSum;
60 FindMaxSubarray (p, n, saveSum );
61 cout <saveSum <endl;
62
63 return 0;
64}

There are two points to note about the above Code:

(1) If all the numbers in an array are negative, then largestSum is still the initial value 0. At this time, the sub-array and the maximum value of the entire array are the largest elements in the array, so we can take it out separately.

(2) For the return type of the function, if it is normal, the return value of the function should be an integer, that is, the range sub-array and the maximum value. However, there is a problem, if the input is invalid, what is returned? If the return value is 0, if the array contains 0 elements and the rest are all negative elements, then 0 indicates invalid input or maximum value? -1? The maximum element in the array is-1. What should I do? In summary, the sub-array and the maximum values are passed into the function parameters in reference mode, and the function range is indicated by whether the function is normal or not, it should be a good method (HE Haitao's best choice is rigorous .)

 

 

References:

He Haitao blog: http://zhedahht.blog.163.com/blog/static/254111742007219147591/

 

Note:

1) All the code environments of this blog are compiled in win7 + VC6. All code has been debugged by the blogger.

2) The blogger python27 has the copyright to this blog post. For the reposted website, please indicate the source at http://www.cnblogs.com/python27 /. You have any suggestions for solving the problem. Please feel free to comment on them.

  

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.