-1

I have an array with numbers in it and want to loop through all combinations of them to calculate a minimum difference between the selected numbers and the rest. How would such a loop look like? I've tried to find a solution, but somehow it doesn't seem to be easy to find such a algorithm.

user
  • 85
  • 7
  • How many items do you have? The number of combinations gets high pretty fast... – Sergey Kalinichenko Sep 22 '12 at 13:15
  • What do you mean by "combination". Do you mean subsets? Pairs? Strings? – Peter Alexander Sep 22 '12 at 13:15
  • Do you want to partition your array in two sets and minimize the difference of their sums ? You should try another approach in this case, search on SO for solutions – Kwariz Sep 22 '12 at 13:18
  • The order of the objects doesn't matter, so that could reduce the amount of combinations. There would be max 40 numbers or so. If there are 10 numbers, one comination could be: numbers 1,3,6 and 2,5,7,8,9,10 or another combination numbers 2,4,5,9 and 1,3,6,7,8 To speed it up a bit I thought I could sum up all numbers first so that I only have to sum up the part with less numbers and subtract that from the total amount to get the other part. Hope my post is not to confusing;) – user Sep 22 '12 at 13:25

1 Answers1

0

To solve your actual problem, i.e.

to calculate a minimum difference between the selected numbers and the rest

or in other words

to select numbers so that the sum of selected is nearest to half of the total sum

or in other words

a variation of the Knapsack problem

Now, you can do a quick search as there are various solutions there.

Dante May Code
  • 11,177
  • 9
  • 49
  • 81