0

Let's say I= have 5 sweets and I want to find all possible combinations of sharing them among my 3 kids.

This will be like something below:

5 for kid_A, 0 for kid_B, 0 for kid_3
0 for kid_A, 5 for kid_B, 0 for kid_3
....
4 for kid_A, 1 for kid_B, 0 for kid_3
4 for kid_A, 0 for kid_B, 1 for kid_3
....
and so on 

Is there an algorithm to find this combinations?

So far, I have managed to compute the first 3 combinations, where I give all my sweets to one of my kids, and the remaining get 0; but I'm lost on how to divide once I'm done with this first iteration.

Gul Ershad
  • 1,592
  • 2
  • 24
  • 28
AliAs
  • 93
  • 1
  • 1
  • 10
  • you can refer the following link https://stackoverflow.com/questions/6772521/calculating-combination-in-java – arjunsv3691 Oct 31 '17 at 16:50
  • 1
    This can easily be done recursively. You can give any number of sweets to the first kid and apply the algorithm recursively to divide the remaining sweets between the remaining kids. – Arnaud Denoyelle Oct 31 '17 at 16:52
  • 1
    @arjunsv3691 The link you gave does not apply to this problem. – Prune Oct 31 '17 at 17:04
  • @rici So I see; thanks. It *is* a dup, but not of that link. This problem *has* been solved on SO in Python and C, and perhaps Java; I didn't look closely enough. – Prune Oct 31 '17 at 17:20
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Oct 31 '17 at 17:21
  • @Al: Do you want to produce all the distributions, or just count the number? – rici Oct 31 '17 at 18:34
  • @rici -- yes, that's a dup. Thanks. Vote to close? I had to rescind mine. – Prune Oct 31 '17 at 18:36

2 Answers2

0

There are three aspects to this problem:

  • How many ways can I give one child up to N sweets? (permutations per child)
  • For each of those, how many ways can I give another child the remaining N sweets? (recursion, maybe)
  • When do I stop dividing out sweets? (termination)

How many ways you can give a child n sweets is simple: you get one permutation for each of 0 -> n, e.g. the first child can be given 0, 1, 2, 3, 4 or 5 sweets.

For the second child, you can repeat this once you subtract the first child's number of sweets. So, for 0 -> m, where m = 5 - n.

For each of these permutations of n and m, the third child simply gets the remainder: t = 5 - (n + m).

From this, you can formulate two loops to generate your permutations. There is a more general case for any amount of children or sweets, but at you have to be careful around your recursing (stack size problems) and aware that for large numbers a number of combinations may be huge.

Danikov
  • 725
  • 3
  • 10
0

Code

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Example {
    public static void main(final String... args) {
        for (final List<Integer> option : divide(5, 3)) {
            System.out.printf("%d for kid_A, %d for kid_B, %d for kid_3%n", option.toArray());
        }
    }

    private static List<List<Integer>> divide(final int count, final int groups) {
        if (groups == 1) {
            final List<Integer> inner = new ArrayList<>(1);
            inner.add(count);
            final List<List<Integer>> outer = new ArrayList<>(1);
            outer.add(inner);
            return outer;
        }
        return IntStream.range(0, count + 1)
            .mapToObj(Integer::valueOf)
            .flatMap(p -> {
                return divide(count - p, groups - 1).stream()
                    .map(q -> {
                        q.add(p);
                        return q;
                    });
            }).collect(Collectors.toList());
    }
}

How it works

This starts for the base assumption that if you have 1 kid, you give them all the sweets.

If you have more than one kid it works out how many to give the first one, and then recurses to find out how many the others can have.

The flatMap gets all the options and adds the number of sweets the current child gets.

jrtapsell
  • 5,698
  • 1
  • 21
  • 46