Subsetsumcpp dry run

could you plz tell me how will these piece of code would dry run?


case 1:-
subsetsum(a,sum+a[i],i+1,n,count);
subsetsum(a,sum,i+1,n,count);


case 2:-
subsetsum(a,i+1,sum,count);
sum+=a[i];
subsetsum(a,i+1,sum,count);

@Kush_1312,
What are you trying to do here in this code? Please tell the context so we can help you dry run it.

Mike is a very passionate about sets. Lately, he is busy solving one of the problems on sets. He has to find whether if the sum of any of the non-empty subsets of the set A is zero.
The question is this.

i wanted to know how would these recursive call would execute?
in case 1 and case2.


here is the codes

@Kush_1312,
Please give a link to the problem as well.

@Kush_1312,
The recursion is pretty straight forward,

  • Standing on a element you have two choices, either to or not to include it in the subset,

Case 1: Include the current element:

This would lead to addition of current element to the subset sum, thus subsetsum(a,sum+a[i],i+1,n,count);

Case 2: Do not include the current element:

This would not affect the subset sum, thus subsetsum(a,sum,i+1,n,count);

Base Case: We run out of elements:

Here we have to decide whether the subset we have made this far should be counted(sum==0) or not(sum!=0), thus
if(i==n){ if(sum==0){ count++; } return; }

would both the statements be executed simultaneously?

or the first statement would reach base case then the compiler would compile the second statement

https://ide.codingblocks.com/s/199819 i have written my doubt as comments

and what would happen if i do that

What was said in video, is correct.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.