I'm trying to write a Java program that, given a particular number of groups and number of total participants, creates a list of all possible ways to fill that number of groups evenly using all the participants. The group number/order doesn't matter. For instance, if {} denotes one possible way to create a set of groups, [] denotes a group, and each individual is represented by a number, the groupings {[1,2],[3,4,5]} and {[4,3,5],[1,2]} should be considered equivalent and are both valid groupings for an input of 5 participants and 2 groups. So far I've written the following code, which works by recursively finding all possible permutations and removing any duplicates (like in the example above). This works fine for small input values, but becomes extremely slow quickly. Can anyone give my any ideas on how to either optimize this code or for a more efficient way to do this (and if the latter, pseudo or java code would be great)? I know that there might not be an extremely fast way to do this, but I'd like to at least be able to run it with some reasonably small numbers and have it finish in a relatively short time period and not take up more RAM than it has too. Let me know if the above explanation is unclear! Thanks!private int numParticipants, numGroups, maxParticipantsPerGroup;private ArrayList<ArrayList<ArrayList<Integer>>> participantGroups = new ArrayList<ArrayList<ArrayList<Integer>>>();private void generateParticipantGroupings() {    ArrayList<ArrayList<Integer>> groupings = new ArrayList<ArrayList<Integer>>();    for (int i = 0; i < numGroups; i++)            groupings.add(new ArrayList<Integer>());    buildParticipantGrouping(1, groupings);}private void buildParticipantGrouping(int currentParticipant, ArrayList<ArrayList<Integer>> grouping) {    if (currentParticipant > numParticipants) {        boolean match = false;        for (int j = 0; j < participantGroups.size(); j++) {            int numMatches = 0;            for (int k = 0; k < numGroups; k++) {                for (int m = 0; m < numGroups; m++) {                    if (grouping.get(k).equals(participantGroups.get(j).get(m))) {                        numMatches++;                        break;                    }                }            }            if (numMatches >= numGroups) {                      match = true;                break;            }        }        if (!match)            participantGroups.add(grouping);    }    else {        for (int i = 0; i < numGroups; i++) {            if (grouping.get(i).size()  < maxParticipantsPerGroup)  {                ArrayList<ArrayList<Integer>> tempGroupings = new ArrayList<ArrayList<Integer>>();                for (int a = 0; a < numGroups; a++) {                    tempGroupings.add(new ArrayList<Integer>());                    for (Integer x : grouping.get(a))                        tempGroupings.get(a).add(x);                }                tempGroupings.get(i).add(currentParticipant);                buildParticipantGrouping(currentParticipant + 1, tempGroupings);            }        }    }}EDIT: Yes, I have searched to see if I can find a previous post that asks the same question. I have not found any. If you can find a previous post asking the same question (that has answers) then I'd love to see it, but otherwise please make sure that a question is actually the same as mine before suggesting it as a duplicate.
Create a list of all possible combinations of elements into n-groups from a set of size k
java;optimization;recursion