This problem can be taken as a variation of pascals identity,
here is the complete code :
please excuse as the code is in java :
public class Combination {
static int count=0;
public static void main(String[] args) {
int a[] = {10, 20, 30, 40, 1, 2,4,11,60,15,5,6};
int setValue = 4;
getCombination(a, setValue);
}
private static void getCombination(int[] a, int setValue) {
// TODO Auto-generated method stub
int size = a.length;
int data[] = new int[setValue];
createCombination(a, data, setValue, 0, 0, size);
System.out.println(" total combinations : "+count);
}
private static void createCombination(int[] a, int[] data, int setValue,
int i, int index, int size) {
// TODO Auto-generated method stub
if (index == setValue) {
if(data[0]+data[1]+data[3]+data[2]==91)
{ count ++;
for (int j = 0; j < setValue; j++)
System.out.print(data[j] + " ");
System.out.println();
}return;
}
// System.out.println(". "+i);
if (i >= size)
return;
// to take care of repetation
if (i < size - 2) {
while (a[i] == a[i + 1])
i++;
}
data[index] = a[i];
// System.out.println(data[index]+" "+index+" .....");
createCombination(a, data, setValue, i + 1, index + 1, size);
createCombination(a, data, setValue, i + 1, index, size);
}
}
Sample input :
int a[] = {10, 20, 30, 40, 1, 2,4,11,60,15,5,6};
Output : :
10 20 1 60
10 30 40 11
10 60 15 6
20 30 40 1
20 60 5 6
30 40 15 6
11 60 15 5
total combinations : 7