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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Jaks_maths Jaks_maths is offline Offline Sep 28th, 2006, 3:56 am |
0
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).
Quick reply to this message  
C Syntax
  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. }
0
manutd manutd is offline Offline | Nov 22nd, 2006
Main really should have a type: int main ( void )
 
0
vocaro vocaro is offline Offline | Apr 6th, 2009
This program is completely wrong. It doesn't even work for the provided test case of (-1, 2, -3, 2, 0, 5, -11).
 
0
jjaspirant jjaspirant is offline Offline | May 25th, 2009
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);

}
 
-1
ravishkkumar ravishkkumar is offline Offline | Oct 27th, 2009
int main(void) :::
no need of it for every time, it is not necessary
 
0
Gaiety Gaiety is offline Offline | Oct 28th, 2009
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....
 
0
yash_2009 yash_2009 is offline Offline | Nov 7th, 2009
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;
}
}
}
 
0
BestJewSinceJC BestJewSinceJC is offline Offline | Nov 8th, 2009
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.
 
0
Iam3R Iam3R is offline Offline | Nov 9th, 2009
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.
 
 

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC