I have a list of items {a,b,c,d} and I need to generate all possible combinations when,
- you can select any number of items
- the order is not important (ab = ba)
- empty set is not considered
If we take the possibilities, it should be,
n=4, number of items
total #of combinations = 4C4 + 4C3 + 4C2 + 4C1 = 15
I used the following recursive method:
private void countAllCombinations (String input,int idx, String[] options) {
for(int i = idx ; i < options.length; i++) {
String output = input + "_" + options[i];
System.out.println(output);
countAllCombinations(output,++idx, options);
}
}
public static void main(String[] args) {
String arr[] = {"A","B","C","D"};
for (int i=0;i<arr.length;i++) {
countAllCombinations(arr[i], i, arr);
}
}
Is there a more efficient way of doing this when the array size is large?