I could not figure out why it gave WA
and what if i had n<=1000 in this case so how to handle the case of negative numbers ??
There is a case when u have not taken any of the elements of the array. You should ignore this case. So, include a counter variable which handles this case.
okayy yaa but how to include this case ??
i mean how to take the variable which knows that it always went to f(i+1,j)
Hey Aryan
What Ankit meant is you have taken a subset with no elements which will always have sum 0 and return 1. So what you can do is add another argument in f function which keeps check of whether some element is included in current subset or not
So function definition will change to f(int i,int j,int c=0)
So this call will change to val = f(i+1,j,c) || f(i+1,j+a[i],1);
Here 3rd arguments will denote if some element is taken or not
Also base case will change accordingly.
thank you sir , got AC