 # recursive sum of subsets problem explaination

can anyone explain me how this piece of code works?

issub(int p[],int n,int sum){

if(sum==0) return true;

else if(n==0 && sum!=0) return false;

if(p[n-1]>sum)

return issub(p,n-1,sum);

else
return issub(p,n-1,sum) || issub(p,n-1,sum-p[n-1]);

}

A = {1,2,3}
B = {2,3,4,D}
D ={2,5,6}
E = {A, B, D}

This is a recursion code and using the sets above as examples in reference to your code. The first method arguments accepts a set in form of an array. issub(int p[],int n,int sum)

Elements of array P can be the values of the set A

n is the number of the elements within the set A

sum is the sum of the elements within set A, in this case 6

So if(sum==0) return true; - This first if statement checks if the set contains any other subsets within it like A B C, If it doesnt then its definately a subset and will return true.

else if(n==0 && sum!=0) return false;- This checks if set contains any elements like 1,2,3 And if the sum of elements it holds is not zero( not empty) thus returning false- not a subset. (The main set could still have elements like (1,2,3) thus having a certain sum.

The last two lines check if the set is a subset of a subset like example B = {2,3,4,D}