943,419 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 15134
  • C RSS
-1

Program to find the largest sum of contiguous integers in the array. O(n)

by on Sep 28th, 2006
Returns the largest sum of contiguous integers in the array
Example: if the input is (-1, 2, -3, 2, 0, 5, -11), the largest sum is 7

Time complexity is O(n).
C Code Snippet (Toggle Plain Text)
  1. #include <stdio.h>
  2. main()
  3. {
  4. int i,j,n,a[20],sum=0,msum =0;
  5.  
  6. scanf("%d",&n);
  7. for(i=1;i<=n;i++)
  8. scanf("%d",&a[i]);
  9.  
  10. for(j=1;j<=n;j++)
  11. {
  12. sum+=a[j];
  13. if(msum<sum)
  14. msum=sum;
  15. if(sum < 0)
  16. sum = 0;
  17. }
  18. printf("\n%d",msum);
  19. }
Comments on this Code Snippet
Nov 22nd, 2006
0

Re: Program to find the largest sum of contiguous integers in the array. O(n)

Main really should have a type: int main ( void )
Junior Poster
manutd is offline Offline
100 posts
since Nov 2006
Apr 6th, 2009
0

Re: Program to find the largest sum of contiguous integers in the array. O(n)

This program is completely wrong. It doesn't even work for the provided test case of (-1, 2, -3, 2, 0, 5, -11).
Newbie Poster
vocaro is offline Offline
1 posts
since Apr 2009
May 25th, 2009
0

Re: Program to find the largest sum of contiguous integers in the array. O(n)

Yes, the solution provided above is incorrect. I came to this website looking for the solution and finally ended up writing it myself. Hope this is useful for someone else.

Here is a working solution. Set the array "a" and N_ELEMENTS accordingly. Some cases are not covered but should be an easy fix.

#include <stdio.h>
#define N_ELEMENTS 7

int main() {
int a[N_ELEMENTS] = {-1, 2, -3, 2, 0, 5, -11 }; // if you change the array, make sure you change N_ELEMENTS
int i = 0;

while(a[i] < 0 && i<N_ELEMENTS) {
i++;
}

if (a[i] < 0) {

printf ("DEBUG: array with only negative numbers. Print the smallest negative number as the sum and we are done.\n");

}

int sum_p=0, sum_n = 0;
int largest_sum = 0;

while (i<N_ELEMENTS) {
if (a[i] > 0) {
sum_p += a[i];

}
else {
sum_n += a[i];

}

if (sum_p+sum_n > largest_sum) {
largest_sum = sum_p + sum_n;

}

if (sum_p+sum_n <= 0) {
// find the next positive number
while(a[i] < 0 && i<N_ELEMENTS) {
i++;
}
if (a[i] < 0 || i == N_ELEMENTS) {
break;

}

sum_p = 0;
sum_n = 0;

} else {
i++;
}
}

printf ("DEBUG: The largest consecutive sum = %d\n", largest_sum);

}
Newbie Poster
jjaspirant is offline Offline
1 posts
since May 2009
Oct 27th, 2009
-1

Re: Program to find the largest sum of contiguous integers in the array. O(n)

int main(void) :::
no need of it for every time, it is not necessary
Newbie Poster
ravishkkumar is offline Offline
3 posts
since Oct 2009
Oct 28th, 2009
0

Re: Program to find the largest sum of contiguous integers in the array. O(n)

what is the meaning of largest sum of contiguous integers.
i understood it as below. ( correct me if wrong )

consider the elements of the array are

-1, 2, -3, 2, 0, 5, -11

so the sums of contiguous elements will be

  (-1) + 2 + (-3) + 2 + 0 + 5 +  (-11)   = -6
            2 + (-3) + 2 + 0 + 5 +  (-11)   = -5
                  (-3) + 2 + 0 + 5 +  (-11)   = -7
                            2 + 0 + 5 +  (-11)   = -4
                                  0 + 5 +  (-11)   = -6


here the largest of all the contiguous elements sums is -4.

if this is the case how can we do it with 0(n) complexity.

consider NOE as no of elements, ele as array with NOE elemensts, sum and large are initially zero.

  1. for ( s = 0; s <NOE-1; s++) {
  2. for ( x= s ; x <NOE-1 ;x++)
  3. sum = sum+ ele[x];
  4. if ( sum > large )
  5. large = sum ;
  6. }

is there any way to do it with less complexity ?

NOTE : not compiled or tested just doc'd the idea....
Junior Poster
Gaiety is offline Offline
107 posts
since Sep 2009
Nov 7th, 2009
-1

Here is a simple solution.

void LargestContiguousSubArray(int[] a, int l, out long sum)
{
sum = -1;
if (0 == l)
{
return;
}

int i = -1;
while (i < l -1
&& 0 >= a[++i])
{
}

long tempSum = 0;

for (; i < l; i++)
{
if (tempSum + a[i] < 0)
{
sum = tempSum;
tempSum = 0;
continue;
}

tempSum += a[i];

if (sum < tempSum)
{
sum = tempSum;
}
}
}
Newbie Poster
yash_2009 is offline Offline
1 posts
since Nov 2009
Nov 8th, 2009
0

Re: Program to find the largest sum of contiguous integers in the array. O(n)

Umm you need a dynamic programming algorithm that uses subproblems and memoization to solve this adequately. Pretty sure all of the "solutions" listed in here are wrong.
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 9th, 2009
0

Re: Program to find the largest sum of contiguous integers in the array. O(n)

i support gaiety's answer with a slight alteration

making the sum zero as the last statement in the first for loop, so that every time it will have a new sum.

if thats not correct can someone provide a correct method.

thank you BestJew, for digging old unsolved thread.
Last edited by Iam3R; Nov 9th, 2009 at 6:05 am.
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Apr 6th, 2010
-1

Re: Program to find the largest sum of contiguous integers in the array. O(n)

I have found a real simple solution.

  1. #include "stdafx.h"
  2. #include"stdio.h"
  3. #include"string.h"
  4. #include"conio.h"
  5.  
  6. int main()
  7. {
  8. int str[20];
  9. int size,i,j,sum,msum;
  10. //str[0]=0;
  11. printf("Enter array size\n");
  12. scanf("%d",&size);
  13. printf("Enter array elements:");
  14. for(i=0;i<size;i++)
  15. {
  16. scanf("%d",&str[i]);
  17. }
  18. msum=0;
  19. for(i=0;i<size;i++)
  20. {
  21. sum=str[i];
  22. for(j=(i+1);j<size;j++)
  23. {
  24. sum=sum+str[j];
  25. if(sum>msum)
  26. {
  27. msum=sum;
  28.  
  29. }
  30. if(sum<0)
  31. sum=0;
  32. }
  33.  
  34. }
  35.  
  36. printf("%d",msum);
  37. getch();
  38. return 0;
  39.  
  40. }
tried and tested :p

cheers..

-Wadoo
Last edited by WaltP; Apr 6th, 2010 at 2:34 pm. Reason: Added CODE Tags
Newbie Poster
wadoobaba is offline Offline
1 posts
since Apr 2010
Message:
Previous Thread in C Forum Timeline: using a char* for file
Next Thread in C Forum Timeline: Help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC