| | |
Program to find the largest sum of contiguous integers in the array. O(n)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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).
Example: if the input is (-1, 2, -3, 2, 0, 5, -11), the largest sum is 7
Time complexity is O(n).
#include <stdio.h> main() { int i,j,n,a[20],sum=0,msum =0; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(j=1;j<=n;j++) { sum+=a[j]; if(msum<sum) msum=sum; if(sum < 0) sum = 0; } printf("\n%d",msum); }
0
•
•
•
•
This program is completely wrong. It doesn't even work for the provided test case of (-1, 2, -3, 2, 0, 5, -11).
0
•
•
•
•
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);
}
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);
}
0
•
•
•
•
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
here the largest of all the contiguous elements sums is -4.
if this is the case how can we do it with
consider
is there any way to do it with less complexity ?
NOTE : not compiled or tested just doc'd the idea....
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) = -6here 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. C Syntax (Toggle Plain Text)
for ( s = 0; s <NOE-1; s++) { for ( x= s ; x <NOE-1 ;x++) sum = sum+ ele[x]; if ( sum > large ) large = sum ; }
is there any way to do it with less complexity ?
NOTE : not compiled or tested just doc'd the idea....
0
•
•
•
•
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;
}
}
}
{
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
•
•
•
•
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
•
•
•
•
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.
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.
Similar Threads
- Using recursion to find largest item in an array (C++)
- C Program to find the largest difference between two prime numbers. (C)
- c++ program to find the product of two array of integers-clear errors? (C++)
- help to sum integers in an array (Java)
- Program to find the largest of given numbers without using loop (Java)
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi



