The and of the largest sub-array

Source: Internet
Author: User

The and of the largest sub-array

In the given array, find the contiguous part of the array, making the elements and the largest. For example, enter 1,-2,5,3,-3,7,-2,-1, the maximum number of sub -Arrays for output, and a.

If nothing is considered, the most straightforward solution is the triple for loop to force the result, the time complexity of the algorithm is O (n^3) code as follows:

//本段代码引自编程之美int MaxSum(int* A, int n){ int maximum = -INF; int sum=0;   for(int i = 0; i < n; i++){ for(int j = i; j < n; j++){  for(int k = i; k <= j; k++)  {   sum += A[k];  

If you do not need to output the maximum Subarray, only the maximum number of sub-arrays and the case, there is a time complexity only O (n) algorithm, the following is the C language implementation code:

void max_sub_array(int count, int* a){    int sum = a[0], t = 0;    for (int i = 0; i < count; i++)    {        if (t < 0)        {            t = a[i];        }        else        {            t += a[i];        }        if (sum < t)            sum = t;    }    printf("The max is %d\n", sum);}void main(){    int count, *p;    printf("please input the count :");    scanf_s("%d", &count);    p = (int *)malloc(count * 2);    printf("\nplease input the number to be sorted : \n");    for (int i = 0; i < count; i++)    {        scanf_s("%d", p+i);    }    max_sub_array(count, p);    system("pause");}

Above this algorithm, the entire array is traversed once, sum is used to record the current maximum and, T is used to record the sum of the sub-array currently being operated on. If t<0 is found, then discard the previously counted sub-arrays, starting with the current A[i], since the previously calculated subarray and negative numbers will not be meaningful anymore. If T>0 or t=0, the sub-array that is being evaluated continues. Finally, the current subarray maximum and (t) and the current maximum and (sum) size are determined until the entire array traversal is complete.

Use a divide-and-conquer strategy to solve the problem, assuming that our original array is A[low,high], first decompose it into two sub-arrays of the same size as possible a[low,mid] and A[mid+1,high]. Then any contiguous subarray of the original array A[i,j] has the following three cases:

    • Completely in the Subarray A[low,mid], i.e. Low<=i<=j<=mid.
    • Completely in the Subarray A{mid+1,high], i.e. Mid+1<=i<=j<=high.
    • Spanning between two sub-arrays, Low<=i<=mid<=j<=high.

So we just need to find out the three and select the largest of them, the time complexity of the algorithm is O (N*LG (n)). The following is the implementation of the C language:

int Max_sub_array (int from, int. to, int* a) {int max = 0;    int Left_max, Right_max, Mid_max;    int Mid_to_left_max = 0, Mid_to_right_max = 0;    int mid_to_left_sum = 0, mid_to_right_sum = 0;    int mid = (to + from)/2;        if (from = = to) {if (A[from] > 0) return a[from];    else return 0;    }//Decomposition of the problem, left and right to solve the maximum and Left_max = Max_sub_array (from, Mid, a);    Right_max = Max_sub_array (mid+1, to, a);        Maximum and processing across the middle for (int i = mid; I >= from;i--) {mid_to_left_sum + = A[i];    if (Mid_to_left_max < mid_to_left_sum) Mid_to_left_max = mid_to_left_sum;        } for (int i = mid + 1, I <= to; i++) {mid_to_right_sum + = A[i];    if (Mid_to_right_max < mid_to_right_sum) Mid_to_right_max = mid_to_right_sum;    }//The maximum and comparison of the three cases will be solved, taking the maximum return Mid_max = Mid_to_left_max + Mid_to_right_max;    max = Left_max;    if (Max < Right_max) max = Right_max; if (Max < Mid_max) max = Mid_max; return Max;}    void Main () {int count, *p;    printf ("Please input the Count:");    scanf_s ("%d", &count);    p = (int *) malloc (count * 2);    printf ("\nplease input the number to be sorted: \ n");    for (int i = 0; i < count; i++) {scanf_s ("%d", p+i);    } printf ("The max is:%d.\n", Max_sub_array (0, Count-1, p)); System ("Pause");}

The and of the largest subarray

Related Article

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.