c - Maximum sum in a Subarray -


you're given array of n integer numbers.
maximal sum of array maximal sum of elements of nonempty consecutive subarray of array.
example, maximal sum of array [1, -2, 3, -2, 5] 6 because sum of subarray [3, -2, 5] 6 , impossible achieve greater subarray sum.
you're allowed remove no more 1 element given array. maximal possible maximal sum of resulting array can achieve doing so?

i testing code own test cases. getting correct output on dev-c++. when test code online wrong answer. not able find out problem.

#include <stdio.h> #include <limits.h> #include <stdlib.h> struct result{     long long int start;     long long int end;     long long int sum; }res; long long int find_max(long long int a[],long long int n) {     long long int max=llong_min;     long long int i;     for(i=0;i<n;++i)     {         if(a[i]>max)             max=a[i];     }     return max; } long long int max_sub(long long int a[],long long int n) {     long long int i;     long long int min,sum1=0;     struct result max,max_curr,*maxsub;     maxsub=calloc(sizeof(res),n);     max.sum=llong_min;     max_curr=max;     for(i=0;i<n;++i)     {         if(max_curr.sum<0)         {             max_curr.sum=a[i];             max_curr.start=i;             max_curr.end=i;         }         else         {             max_curr.sum+=a[i];             max_curr.end=i;         }         if(max_curr.sum>max.sum)         {             max=max_curr;         }         maxsub[i]=max;     }     min=0;     for(i=maxsub[n-1].start;i<=maxsub[n-1].end;++i)     {         if(a[i]<0)         {             if(min==0 || a[i]<min)                 min=a[i];         }     }     sum1=maxsub[n-1].sum-min;     return sum1; } int main() {     int t;     scanf("%d",&t);     while(t--){         long long int n,i;         scanf("%lld",&n);         long long int a[n];         for(i=0;i<n;++i)             scanf("%lld",&a[i]);         long long int sum=0;         sum=find_max(a,n);         if(sum<=0)         {             printf("%lld\n",sum);         }         else         {             sum=max_sub(a,n);             printf("%lld\n",sum);         }      }     return 0; } 

please close thread. op trying cheat in online competition going on right now, posting questions here.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -