You're given an array containing both positive and negative integers
and required to find the sub-array with the largest sum (O(N) a la
KBL). Write a routine in C for the above
Any pointers on the above problem will help.
Wont sum of all positive numbers will be the largest sub-array?
Strictly speaking this is OT for c.l.c, but it came up in another
newsgroup a while ago, and I published this solution. I believe it
to be accurate, but have not proven so. Have at it.
/* How to find the maximum sum of at least L consecutive
integers given a sequence of N integers. */
/* Public Domain by CB Falconer, 2006-03-06 */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAXRUN 100
typedef int (*instream)(void); /* how to get values */
/* -------------- */
void help(void)
{
fprintf(stderr, "Usage: maxsum <n> <l> [any]\n"
"where n < %d and l < n\n"
"Finds maximum sum of at least l consecutive\n"
"integers in a sequence of n integers\n"
"\n[any] entry enables debug display\n",
MAXRUN);
exit(EXIT_FAILURE);
} /* help */
/* -------------- */
int getint(char *s)
{
long test;
char *endptr;
test = strtol(s, &endptr, 10);
if ((test > MAXRUN) || (test <= 0)) help(); /* and exit */
return test;
} /* getint */
/* -------------- */
int column;
/* setting column < 0 inhibits all display */
void showitem(int item, int itemcnt)
{
if (column >= 0) {
if (!itemcnt) column += printf("%10d ", item);
else column += printf("%10d,%-2d", item, itemcnt);
if (column > 64) {
column = 0; putchar('\n');
}
}
} /* showitem */
/* -------------- */
size_t maxcount;
#define initnext(count) maxcount = count
/* get the next integer from the input stream */
int next(void)
{
int value;
if (!maxcount) return EOF;
else maxcount--;
do {
value = (rand() % 65) - 32; /* symettric about 0 */
} while (EOF == value);
return value;
} /* next */
/* -------------- */
/* A list of these items holds the pertinent input history */
struct sofar {
int sumtohere;
struct sofar *next;
};
/* -------------- */
struct sofar *discard(struct sofar *trail)
{
struct sofar *t;
t = trail->next;
free(trail);
return t;
} /* discard */
/* -------------- */
struct sofar *newitem(int value)
{
struct sofar *item;
if (!(item = malloc(sizeof *item))) {
fprintf(stderr, "Out of memory, halting\n");
exit(EXIT_FAILURE);
}
item->sumtohere = value;
item->next = NULL;
return item;
} /* newitem */
/* -------------- */
/* solve the real problem */
int solve(instream getnext, int l)
{
int v, items;
int sum, maxsum;
struct sofar *trail, *current, *temp;
/* initialize detection */
current = trail = newitem(0);
sum = maxsum = items = 0;
while (EOF != (v = getnext())) {
/* process v into the system */
items++;
temp = newitem(v + current->sumtohere);
current->next = temp;
current = temp;
while (trail->next->sumtohere <= trail->sumtohere
&& items > l) {
/* oldest input value is negative or zero so */
/* the sum can only be imcreased by discard */
trail = discard(trail);
items--;
}
sum = current->sumtohere - trail->sumtohere;
while ((sum <= 0) && (items > l)) {
/* The only purpose of the older entries is */
/* to enable inclusion of a future series */
/* without demanding the item count start */
/* from this current point. This may drive */
/* the sum more negative. The preceding */
/* while loop will eventually handle that */
trail = discard(trail);
items--;
sum = current->sumtohere - trail->sumtohere;
}
showitem(v, 0);
if ((sum >= maxsum) && (items >= l)) {
maxsum = sum;
showitem(sum, items);
}
}
return maxsum;
} /* solve */
/* -------------- */
int main(int argc, char **argv)
{
int n, l;
int ans;
if (argc < 3) help();
else {
if (3 == argc) column = -1; /* inhibit debug */
n = getint(argv[1]);
l = getint(argv[2]);
if (l > n) help();
initnext(n);
ans = solve(next, l);
if (column > 0) putchar('\n');
printf("Max. sum is %d\n", ans);
}
return 0;
} /* main */
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <
http://cfaj.freeshell.org/google/>
Also see <
http://www.safalra.com/special/googlegroupsreply/>