pass one more argument to the function "*int index*" and instead of starting the loop from *i = 0 to N, *make it start from *i = index to N *and then call
*printcombinations(a,sum-a[i],level+1,index+1); *I think it will work then... On Thu, Jun 23, 2011 at 10:48 AM, ross <jagadish1...@gmail.com> wrote: > 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. > > -- *Piyush Sinha* *IIIT, Allahabad* *+91-8792136657* *+91-7483122727* *https://www.facebook.com/profile.php?id=100000655377926 * -- 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.