Maximum and [algorithm] for sub-arrays

Source: Internet
Author: User

               

Title: Enter an array of shapes with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array, each of which has a and. All sub-arrays are evaluated

and the maximum value. Requires a time complexity of O (n).

For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the largest sub-array is 3, 10,-4, 7, 2, so the output is the and 18 of that subarray.

My first feeling is that the 3-layer for loop goes directly, like a violent for loop, traverse all possible, or your own idea too low

, in fact, the complexity of the time of the problem can be changed to O (n), the following code with 3 different time to complete the complexity of the problem, so the code to optimize, but also to ask others, their code is not necessarily very good. Nonsense is not much to say, because the algorithm is relatively simple, so do not explain more.

1 time complexity of O (n^3)

#include <stdio.h> #include <stdlib.h>int main () {int n,t;int *a;int i,j;int x=0,y=0,m;int max=0,sum=0;scanf ("%d", &n), a= (int *) malloc (sizeof (int) *n), for (i=0;i<n;i++)  scanf ("%d", a+i), and for (i=0;i<n;i++)        { For       (j=0;j<n;j++)       {         m=j;         sum=0;     for (int k=0;k<i;k++)       sum+=a[m++];  if (max<sum)    {     max=sum;     x=i;     Y=j;}}}       printf ("x=%d,y=%d\n", X, Y), if (x>y) {t=x;x=y;y=t;}      If X>y  , do the exchange for   (i=x;i<=y+1;i++)        //Here +1, because the array although the number of tokens plus +1 is the actual number    


2 time complexity of O (n^2)

#include <stdio.h> #include <stdlib.h>int main () {int n;int *a;int i,j;int sum=0,max=0;scanf ("%d", &n); a = (int *) malloc (sizeof (int) *n), for (i=0;i<n;i++)  scanf ("%d", a+i), and for (i=0;i<n;i++) {sum=0      ;  sum 0 for   (j=i;j<n;j++)  {     sum+=a[j]     each time; if (max<sum)        max=sum;     


Time Complexity of O (n)

#include <stdio.h> #include <stdlib.h>int main () {int n;int *a;int i;int sum=0,bettersum=0;scanf ("%d",& n); a= (int *) malloc (sizeof (int) *n); for (i=0;i<n;i++)  scanf ("%d", a+i)    ; for (i=0;i<n;i++) {           sum+=a[i];           If the current and value are zero, the current and value are set to zero if    (sum<0)      sum=0;//If the current and value is greater than the optimal value, then the optimal value records the current and value    if (bettersum<sum)      bettersum=sum;               }    If the array is all negative, you only need to find a maximum negative if (0==bettersum) {bettersum=a[0];for (i=0;i<n;i++) {if (a[i]>bettersum)  Bettersum=a[i];}}    


Maximum and [algorithm] for sub-arrays

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.