Calculate the sum of the largest sub-array and the start position and end position of the largest sub-array.

Source: Internet
Author: User
Tags use definition

Problem description:

An array with a length of N. the array elements have positive values, such as {-1, 4, 6,-3, 7,-3,-3, 9 }; we can clearly know that the largest sub-array should be 4 to 9, that is, subscript 1 to subscript 7, and the sum is 17.

Solution: the first method: We can use Definition 1 and two numbers ThisSum and MaxSum to record the sum of the current array and the largest sum of the array.
2. We can use two for loops to traverse the array. Each time we obtain the largest sum of sub-arrays, each sub-array starts from 0, and the next traversal sub-array starts from 1, and so on. For example, for the first time ~ N-1] of the largest and, the second is [1 ~ N-1, the third time is [2 ~ N ]....
3. In this way, ThisSum is used to record the sum of the current array, and MAXSum is used to record the largest sum of the current sub-array, which is compared with ThisSum, obtain the sum of the largest sub-array from the maximum value.
4. Check the code for better understanding:
 
int MaxSubArraySum(int a[], int N, int &start, int &end){int ThisSum, MaxSum, i, j;*start = 0;*end = 0;MaxSum = 0;for (i = 0; i < N; ++i){ThisSum = 0;for (j = i; j < N; ++j){ThisSum += a[j];if (ThisSum > MaxSum){*start  = i;*end = j;MaxSum = ThisSum;}}}return MaxSum;}
5. the time complexity of this method is obvious (O (N ^ 2 ))
The second algorithm: Train of Thought: we strive for a method with the highest force grid to make the time complexity (O (N )). 1. Similarly, we use ThisSum to record the sum of the current sub-array, and MaxSum to record the largest sum of the current sub-array. We can start from 0, start traversing every time ThisSum is added with a [I]. If the current ThisSum is greater than MaxSum, the value of ThisSum is paid to MaxSum. If the current ThisSum is <0, the value of ThisSum is assigned to 0, next, we will traverse the next step.
2. Here I have to talk about how to find the method from the start point to the end of the largest subarray, a. We define start and end to record B. When thisSum> maxSum, the end of the record is equal to the subscript at this time. C. When thisSum is <0, we can know that the start point must be changed from the new one to the next subscript (I + 1) as the start point, but be careful when the next subscript (I + 1) the element value may be less than 0 or out of bounds, so we must judge. For example, if (I <= arr. length-2 & arr [I + 1]> 0 ){
Start = I + 1;
} We can write the code:
Int maxSubArraySum (int [] arr, int N, int & start, int end) {int maxSum = 0; int thisSum = 0; int I; * start = 0; * end = 0; for (I = 0; I <N; I ++) {thisSum + = arr [I]; if (thisSum> maxSum) {maxSum = thisSum; * end = I;} if (thisSum <0) {thisSum = 0; if (I <= N-2 & arr [I + 1]> 0) {* start = I + 1 ;}}// if the largest sub-array is not in the array (all array elements <= 0), start, end value:-1if (* start = 0 & * end = 0 & arr [0] <= 0) {* start =-1; * end =-1;} return maxSum ;}


The time complexity is (O (N ).

Conclusion: In chapter 2 of "data structure and algorithm analysis: C language description", this question appears, author from (O (N ^ 3 )) I have to admire O (N). There are both useful loops and recursion. Today I only talk about two typical methods.
Another thing is that the author did not find the starting point and the end of the sub-array, so I tried it, haha.



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.