user3070528
user3070528

Reputation: 87

Finding all possible sums of a set

I am currently looking for ideas on how to find all possible sums of a set of numbers with these rules. I have these numbers to work with and I want to find all possible sums so that you can only use a single number at max 4 times and each time you pick 7 of these numbers.

{ 0, 1, 5, 22, 98, 453, 2031, 8698, 22854, 83661, 262349, 636345 and 1479181 }

Acceptable examples would be

0 + 0 + 0 + 0 + 83661 + 83661 + 2031

Unacceptable example would be

0 + 0 + 0 + 0 + 0 + 83661 + 2031

The only way I can think of is a series of nested loops but I am having trouble with that as well. Would there be any other options to do this. I am using Java but I don't really think that matters.

Upvotes: 5

Views: 1401

Answers (1)

Mifmif
Mifmif

Reputation: 3190

You can achieve this by building a new List of elements that contains each element of the given set duplicated 4 times . then use a DFS strategy method to build possible possible sum combination. to have an idea about how to implement DFS check this answer

Upvotes: 4

Related Questions