Search code examples
algorithmloopscombinationsminimum

All combinations of objects in array


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.


Solution

  • 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.