Re: array of integers
- From: "Kemal Oral CANSIZLAR" <ocansizlar@xxxxxxxxxxx>
- Date: Fri, 29 Apr 2005 02:35:43 -0700
Hi,
----- Original Message -----
From: "Willem" <willem@xxxxxxxx>
Newsgroups: comp.programming
Sent: Friday, April 29, 2005 1:31 AM
Subject: Re: array of integers
> Arthur wrote:
> )
> ) On Thu, 28 Apr 2005, Kemal Oral CANSIZLAR wrote:
> )>
> )> There is an array of integers, without any restriction. How can I come
> up
> )> with an algorithm to find all combinations of elements that add up to
> the
> )> numbers in the array. If array is {1, 3, 2, 5, 4}, result would be {1,
> 2} =
> )> 3 is in array; {1, 3} = 4 is in array; {2, 3} = 5 is in array; {1, 4} =
> 5 is
> )> in array.
> )
> ) That depends in what format your input and output need to be. I
> think
> ) (but I'm no expert) that if you're given a set (say {1,3,6,7}) and some
> ) value (say 8) by the user, and asked "does any subset of {1,3,6,7} sum
> ) to 8?", then that's a variant of the subset-sum problem, which is hard.
> ) (For the definition of "hard" in this context, Google "NP-complete".)
>
> That's almost what he wants, just that he wants it not for a single
> number,
> but for a set of numbers (namely, the array).
>
> I thought he'd made that clear enough: he wants all subsets of the array
> that sum to an element of the array.
>
Thanks for the clarification Willem. The paraphrase of yours is better; "all
subsets of the array that sum to an element of the array"
I am convinced the problem is NP-complete.
And inclusion of negative values make the problem even harder to tackle with
if you were to think of efficient ways.
I think I should impose a restriction on input, namely, allowing only
positive integers.
I am guessing CBFalconer's code, with a little modification, could at least
give me an approximate result (if not exhaustive listing of all
combinations) for the positive integers.
The other way is of course trying all 2^n subsets starting from one element
subsets growing towards the n elements, maybe equipped with some early
detection of impossible subsets (assuming the set is sorted). Moreover using
hash-tables may also prove useful as Thad mentioned.
>
> SaSW, Willem
> --
> Disclaimer: I am in no way responsible for any of the statements
> made in the above text. For all I know I might be
> drugged or something..
> No I'm not paranoid. You all think I'm paranoid, don't you !
> #EOT
.
- Follow-Ups:
- Re: array of integers
- From: moi
- Re: array of integers
- References:
- array of integers
- From: Kemal Oral CANSIZLAR
- Re: array of integers
- From: Arthur J. O'Dwyer
- Re: array of integers
- From: Willem
- array of integers
- Prev by Date: Re: array of integers
- Next by Date: Re: Writing a character to the beginning of the same file
- Previous by thread: Re: array of integers
- Next by thread: Re: array of integers
- Index(es):
Relevant Pages
|