# H interview (23): the largest and

Source: Internet
Author: User

Description:
Enter an integer array, and the array contains positive and negative numbers.
One or more consecutive integers in the array form a sub-array. Each sub-array has a sum.
Returns the maximum value of the sum of all sub-arrays. The time complexity is O (n ).

For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the maximum sub-array is 3, 10,-4, 7, 2,
Therefore, the output is the sum of 18 of the sub-array.

`# Include <stdio. h> # include <malloc. h> # include <assert. h> int max_subarray_sum (int * a, int num) {assert (a); int sum = 0; // traverses the array value and stores the value int max = A ; // store the maximum value of the current sub-array Int J = 0; // record the position of the current I int I; // traverse the array counter for (I = 0; I <num;) // cyclically traverse the Array {sum = sum + A [I]; If (sum> MAX) // if the sum result is greater than Max, update Max, record the position of the current I {max = sum; I ++; j = I;} else if (sum <0) // if the sum value is less than 0, it indicates that the result of adding the previous sub-array is not ideal. sum is set to zero, and {sum = 0; I = J; I ++;} is recalculated from the next I ;} else // This indicates that the sum value is not Max, but the sum value is not smaller than zero: A non-positive number may be a larger integer, so continue the sequential I ++; j = I;} if (I = num-1 & max <0) // This can avoid all negative cases {for (I = 1; I <num; I ++) {if (a [I]> MAX) max = A [I] ;}} return Max ;} int main () {int A [] = {-3,-2, 0}; int num = sizeof (a)/sizeof (INT); int max_subarray_sum = max_subarray_sum (A, num ); printf ("% d \ n", max_subarrya_sum); Return 0 ;}`

`// Copyright @ July // July, updated, 2011.05.25. # Include <iostream. h> # define N 4 // define an extra variable int maxsum (int A [n]) // here, you can see the above idea 2 code (pointer) {int max = A ; // returns the maximum number of int sum = 0; For (Int J = 0; j <n; j ++) {If (sum> = 0) // If an element is added, sum> = 0, sum + = A [J]; else sum = A [J]; // If an element is added and sum <0, if (sum> MAX) max = sum;} return Max;} int main () is not added () {int A [] = {-1,-2,-3,-4}; cout <maxsum (a) <Endl; return 0 ;}`

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.