Finding all possible combinations of numbers to reach a given sum

前端 未结 30 3019
一个人的身影
一个人的身影 2020-11-21 06:39

How would you go about testing all possible combinations of additions from a given set N of numbers so they add up to a given final number?

A brief exam

相关标签:
30条回答
  • 2020-11-21 07:13

    Java non-recursive version that simply keeps adding elements and redistributing them amongst possible values. 0's are ignored and works for fixed lists (what you're given is what you can play with) or a list of repeatable numbers.

    import java.util.*;
    
    public class TestCombinations {
    
        public static void main(String[] args) {
            ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(0, 1, 2, 2, 5, 10, 20));
            LinkedHashSet<Integer> targets = new LinkedHashSet<Integer>() {{
                add(4);
                add(10);
                add(25);
            }};
    
            System.out.println("## each element can appear as many times as needed");
            for (Integer target: targets) {
                Combinations combinations = new Combinations(numbers, target, true);
                combinations.calculateCombinations();
                for (String solution: combinations.getCombinations()) {
                    System.out.println(solution);
                }
            }
    
            System.out.println("## each element can appear only once");
            for (Integer target: targets) {
                Combinations combinations = new Combinations(numbers, target, false);
                combinations.calculateCombinations();
                for (String solution: combinations.getCombinations()) {
                    System.out.println(solution);
                }
            }
        }
    
        public static class Combinations {
            private boolean allowRepetitions;
            private int[] repetitions;
            private ArrayList<Integer> numbers;
            private Integer target;
            private Integer sum;
            private boolean hasNext;
            private Set<String> combinations;
    
            /**
             * Constructor.
             *
             * @param numbers Numbers that can be used to calculate the sum.
             * @param target  Target value for sum.
             */
            public Combinations(ArrayList<Integer> numbers, Integer target) {
                this(numbers, target, true);
            }
    
            /**
             * Constructor.
             *
             * @param numbers Numbers that can be used to calculate the sum.
             * @param target  Target value for sum.
             */
            public Combinations(ArrayList<Integer> numbers, Integer target, boolean allowRepetitions) {
                this.allowRepetitions = allowRepetitions;
                if (this.allowRepetitions) {
                    Set<Integer> numbersSet = new HashSet<>(numbers);
                    this.numbers = new ArrayList<>(numbersSet);
                } else {
                    this.numbers = numbers;
                }
                this.numbers.removeAll(Arrays.asList(0));
                Collections.sort(this.numbers);
    
                this.target = target;
                this.repetitions = new int[this.numbers.size()];
                this.combinations = new LinkedHashSet<>();
    
                this.sum = 0;
                if (this.repetitions.length > 0)
                    this.hasNext = true;
                else
                    this.hasNext = false;
            }
    
            /**
             * Calculate and return the sum of the current combination.
             *
             * @return The sum.
             */
            private Integer calculateSum() {
                this.sum = 0;
                for (int i = 0; i < repetitions.length; ++i) {
                    this.sum += repetitions[i] * numbers.get(i);
                }
                return this.sum;
            }
    
            /**
             * Redistribute picks when only one of each number is allowed in the sum.
             */
            private void redistribute() {
                for (int i = 1; i < this.repetitions.length; ++i) {
                    if (this.repetitions[i - 1] > 1) {
                        this.repetitions[i - 1] = 0;
                        this.repetitions[i] += 1;
                    }
                }
                if (this.repetitions[this.repetitions.length - 1] > 1)
                    this.repetitions[this.repetitions.length - 1] = 0;
            }
    
            /**
             * Get the sum of the next combination. When 0 is returned, there's no other combinations to check.
             *
             * @return The sum.
             */
            private Integer next() {
                if (this.hasNext && this.repetitions.length > 0) {
                    this.repetitions[0] += 1;
                    if (!this.allowRepetitions)
                        this.redistribute();
                    this.calculateSum();
    
                    for (int i = 0; i < this.repetitions.length && this.sum != 0; ++i) {
                        if (this.sum > this.target) {
                            this.repetitions[i] = 0;
                            if (i + 1 < this.repetitions.length) {
                                this.repetitions[i + 1] += 1;
                                if (!this.allowRepetitions)
                                    this.redistribute();
                            }
                            this.calculateSum();
                        }
                    }
    
                    if (this.sum.compareTo(0) == 0)
                        this.hasNext = false;
                }
                return this.sum;
            }
    
            /**
             * Calculate all combinations whose sum equals target.
             */
            public void calculateCombinations() {
                while (this.hasNext) {
                    if (this.next().compareTo(target) == 0)
                        this.combinations.add(this.toString());
                }
            }
    
            /**
             * Return all combinations whose sum equals target.
             *
             * @return Combinations as a set of strings.
             */
            public Set<String> getCombinations() {
                return this.combinations;
            }
    
            @Override
            public String toString() {
                StringBuilder stringBuilder = new StringBuilder("" + sum + ": ");
                for (int i = 0; i < repetitions.length; ++i) {
                    for (int j = 0; j < repetitions[i]; ++j) {
                        stringBuilder.append(numbers.get(i) + " ");
                    }
                }
                return stringBuilder.toString();
            }
        }
    }
    

    Sample input:

    numbers: 0, 1, 2, 2, 5, 10, 20
    targets: 4, 10, 25
    

    Sample output:

    ## each element can appear as many times as needed
    4: 1 1 1 1 
    4: 1 1 2 
    4: 2 2 
    10: 1 1 1 1 1 1 1 1 1 1 
    10: 1 1 1 1 1 1 1 1 2 
    10: 1 1 1 1 1 1 2 2 
    10: 1 1 1 1 2 2 2 
    10: 1 1 2 2 2 2 
    10: 2 2 2 2 2 
    10: 1 1 1 1 1 5 
    10: 1 1 1 2 5 
    10: 1 2 2 5 
    10: 5 5 
    10: 10 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 
    25: 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 
    25: 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 
    25: 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 
    25: 1 1 1 2 2 2 2 2 2 2 2 2 2 2 
    25: 1 2 2 2 2 2 2 2 2 2 2 2 2 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 5 
    25: 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 5 
    25: 1 1 1 1 1 1 1 1 2 2 2 2 2 2 5 
    25: 1 1 1 1 1 1 2 2 2 2 2 2 2 5 
    25: 1 1 1 1 2 2 2 2 2 2 2 2 5 
    25: 1 1 2 2 2 2 2 2 2 2 2 5 
    25: 2 2 2 2 2 2 2 2 2 2 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 2 5 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 2 2 5 5 
    25: 1 1 1 1 1 1 1 1 1 2 2 2 5 5 
    25: 1 1 1 1 1 1 1 2 2 2 2 5 5 
    25: 1 1 1 1 1 2 2 2 2 2 5 5 
    25: 1 1 1 2 2 2 2 2 2 5 5 
    25: 1 2 2 2 2 2 2 2 5 5 
    25: 1 1 1 1 1 1 1 1 1 1 5 5 5 
    25: 1 1 1 1 1 1 1 1 2 5 5 5 
    25: 1 1 1 1 1 1 2 2 5 5 5 
    25: 1 1 1 1 2 2 2 5 5 5 
    25: 1 1 2 2 2 2 5 5 5 
    25: 2 2 2 2 2 5 5 5 
    25: 1 1 1 1 1 5 5 5 5 
    25: 1 1 1 2 5 5 5 5 
    25: 1 2 2 5 5 5 5 
    25: 5 5 5 5 5 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 
    25: 1 1 1 1 1 1 1 1 1 1 1 1 1 2 10 
    25: 1 1 1 1 1 1 1 1 1 1 1 2 2 10 
    25: 1 1 1 1 1 1 1 1 1 2 2 2 10 
    25: 1 1 1 1 1 1 1 2 2 2 2 10 
    25: 1 1 1 1 1 2 2 2 2 2 10 
    25: 1 1 1 2 2 2 2 2 2 10 
    25: 1 2 2 2 2 2 2 2 10 
    25: 1 1 1 1 1 1 1 1 1 1 5 10 
    25: 1 1 1 1 1 1 1 1 2 5 10 
    25: 1 1 1 1 1 1 2 2 5 10 
    25: 1 1 1 1 2 2 2 5 10 
    25: 1 1 2 2 2 2 5 10 
    25: 2 2 2 2 2 5 10 
    25: 1 1 1 1 1 5 5 10 
    25: 1 1 1 2 5 5 10 
    25: 1 2 2 5 5 10 
    25: 5 5 5 10 
    25: 1 1 1 1 1 10 10 
    25: 1 1 1 2 10 10 
    25: 1 2 2 10 10 
    25: 5 10 10 
    25: 1 1 1 1 1 20 
    25: 1 1 1 2 20 
    25: 1 2 2 20 
    25: 5 20 
    ## each element can appear only once
    4: 2 2 
    10: 1 2 2 5 
    10: 10 
    25: 1 2 2 20 
    25: 5 20
    
    0 讨论(0)
  • 2020-11-21 07:13

    Deduce 0 in the first place. Zero is an identiy for addition so it is useless by the monoid laws in this particular case. Also deduce negative numbers as well if you want to climb up to a positive number. Otherwise you would also need subtraction operation.

    So... the fastest algorithm you can get on this particular job is as follows given in JS.

    function items2T([n,...ns],t){
        var c = ~~(t/n);
        return ns.length ? Array(c+1).fill()
                                     .reduce((r,_,i) => r.concat(items2T(ns, t-n*i).map(s => Array(i).fill(n).concat(s))),[])
                         : t % n ? []
                                 : [Array(c).fill(n)];
    };
    
    var data = [3, 9, 8, 4, 5, 7, 10],
        result;
    
    console.time("combos");
    result = items2T(data, 15);
    console.timeEnd("combos");
    console.log(JSON.stringify(result));

    This is a very fast algorithm but if you sort the data array descending it will be even faster. Using .sort() is insignificant since the algorithm will end up with much less recursive invocations.

    0 讨论(0)
  • 2020-11-21 07:14

    Perl version (of the leading answer):

    use strict;
    
    sub subset_sum {
      my ($numbers, $target, $result, $sum) = @_;
    
      print 'sum('.join(',', @$result).") = $target\n" if $sum == $target;
      return if $sum >= $target;
    
      subset_sum([@$numbers[$_ + 1 .. $#$numbers]], $target, 
                 [@{$result||[]}, $numbers->[$_]], $sum + $numbers->[$_])
        for (0 .. $#$numbers);
    }
    
    subset_sum([3,9,8,4,5,7,10,6], 15);
    

    Result:

    sum(3,8,4) = 15
    sum(3,5,7) = 15
    sum(9,6) = 15
    sum(8,7) = 15
    sum(4,5,6) = 15
    sum(5,10) = 15
    

    Javascript version:

    const subsetSum = (numbers, target, partial = [], sum = 0) => {
      if (sum < target)
        numbers.forEach((num, i) =>
          subsetSum(numbers.slice(i + 1), target, partial.concat([num]), sum + num));
      else if (sum == target)
        console.log('sum(%s) = %s', partial.join(), target);
    }
    
    subsetSum([3,9,8,4,5,7,10,6], 15);

    Javascript one-liner that actually returns results (instead of printing it):

    const subsetSum=(n,t,p=[],s=0,r=[])=>(s<t?n.forEach((l,i)=>subsetSum(n.slice(i+1),t,[...p,l],s+l,r)):s==t?r.push(p):0,r);
    
    console.log(subsetSum([3,9,8,4,5,7,10,6], 15));

    And my favorite, one-liner with callback:

    const subsetSum=(n,t,cb,p=[],s=0)=>s<t?n.forEach((l,i)=>subsetSum(n.slice(i+1),t,cb,[...p,l],s+l)):s==t?cb(p):0;
    
    subsetSum([3,9,8,4,5,7,10,6], 15, console.log);

    0 讨论(0)
  • 2020-11-21 07:15

    Here's a solution in R

    subset_sum = function(numbers,target,partial=0){
      if(any(is.na(partial))) return()
      s = sum(partial)
      if(s == target) print(sprintf("sum(%s)=%s",paste(partial[-1],collapse="+"),target))
      if(s > target) return()
      for( i in seq_along(numbers)){
        n = numbers[i]
        remaining = numbers[(i+1):length(numbers)]
        subset_sum(remaining,target,c(partial,n))
      }
    }
    
    0 讨论(0)
  • 2020-11-21 07:16

    Here is a Java version which is well suited for small N and very large target sum, when complexity O(t*N) (the dynamic solution) is greater than the exponential algorithm. My version uses a meet in the middle attack, along with a little bit shifting in order to reduce the complexity from the classic naive O(n*2^n) to O(2^(n/2)).

    If you want to use this for sets with between 32 and 64 elements, you should change the int which represents the current subset in the step function to a long although performance will obviously drastically decrease as the set size increases. If you want to use this for a set with odd number of elements, you should add a 0 to the set to make it even numbered.

    import java.util.ArrayList;
    import java.util.List;
    
    public class SubsetSumMiddleAttack {
        static final int target = 100000000;
        static final int[] set = new int[]{ ... };
    
        static List<Subset> evens = new ArrayList<>();
        static List<Subset> odds = new ArrayList<>();
    
        static int[][] split(int[] superSet) {
            int[][] ret = new int[2][superSet.length / 2]; 
    
            for (int i = 0; i < superSet.length; i++) ret[i % 2][i / 2] = superSet[i];
    
            return ret;
        }
    
        static void step(int[] superSet, List<Subset> accumulator, int subset, int sum, int counter) {
            accumulator.add(new Subset(subset, sum));
            if (counter != superSet.length) {
                step(superSet, accumulator, subset + (1 << counter), sum + superSet[counter], counter + 1);
                step(superSet, accumulator, subset, sum, counter + 1);
            }
        }
    
        static void printSubset(Subset e, Subset o) {
            String ret = "";
            for (int i = 0; i < 32; i++) {
                if (i % 2 == 0) {
                    if ((1 & (e.subset >> (i / 2))) == 1) ret += " + " + set[i];
                }
                else {
                    if ((1 & (o.subset >> (i / 2))) == 1) ret += " + " + set[i];
                }
            }
            if (ret.startsWith(" ")) ret = ret.substring(3) + " = " + (e.sum + o.sum);
            System.out.println(ret);
        }
    
        public static void main(String[] args) {
            int[][] superSets = split(set);
    
            step(superSets[0], evens, 0,0,0);
            step(superSets[1], odds, 0,0,0);
    
            for (Subset e : evens) {
                for (Subset o : odds) {
                    if (e.sum + o.sum == target) printSubset(e, o);
                }
            }
        }
    }
    
    class Subset {
        int subset;
        int sum;
    
        Subset(int subset, int sum) {
            this.subset = subset;
            this.sum = sum;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:17

    Swift 3 conversion of Java solution: (by @JeremyThompson)

    protocol _IntType { }
    extension Int: _IntType {}
    
    
    extension Array where Element: _IntType {
    
        func subsets(to: Int) -> [[Element]]? {
    
            func sum_up_recursive(_ numbers: [Element], _ target: Int, _ partial: [Element], _ solution: inout [[Element]]) {
    
                var sum: Int = 0
                for x in partial {
                    sum += x as! Int
                }
    
                if sum == target {
                    solution.append(partial)
                }
    
                guard sum < target else {
                    return
                }
    
                for i in stride(from: 0, to: numbers.count, by: 1) {
    
                    var remaining = [Element]()
    
                    for j in stride(from: i + 1, to: numbers.count, by: 1) {
                        remaining.append(numbers[j])
                    }
    
                    var partial_rec = [Element](partial)
                    partial_rec.append(numbers[i])
    
                    sum_up_recursive(remaining, target, partial_rec, &solution)
                }
            }
    
            var solutions = [[Element]]()
            sum_up_recursive(self, to, [Element](), &solutions)
    
            return solutions.count > 0 ? solutions : nil
        }
    
    }
    

    usage:

    let numbers = [3, 9, 8, 4, 5, 7, 10]
    
    if let solution = numbers.subsets(to: 15) {
        print(solution) // output: [[3, 8, 4], [3, 5, 7], [8, 7], [5, 10]]
    } else {
        print("not possible")
    }
    
    0 讨论(0)
提交回复
热议问题