Given an array and a sum S output all combinations of elements that sum to S. eg: 1 2 3
sum = 3 1+1+1, 2+1 3 I came up with the foll algorithm, but it outputs 2+1 and 1+2 again. (does not handle repetitions) printcombinations(int a[],int sum,int level) { if(sum==0) { print array} else if (sum>0) { for ( i = 0 to N ) { array[level]=a[i]; printcombinations(a,sum-a[i],level+1); } } } -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.