Code is compiling but no output
- In your base case , if(i==n) , if the sum==0 condition is not hit and true isn’t returned , you should return false after that or your code tries to move even further i=n and goes into an infinite loop state.
- Line No. 8 , for loop , increment k instead of i.
- Line No. 16 , you are assigning values of array a to a only. Since a is empty , it is never modified and values of array ‘arr’ are practically useless.
Modify this statement to
a[j]=arr[i]; - After these changes , your code will start producing outputs atleast but it will always be “No” , regardless of the inputs. This is because the result produced in recursive calls isn’t returned to parent recursive calls i.e. when you make a recursive call in your function , you should store the result obtained from it and do something with it. You are simply making those recursive calls and returning false at the end of the function regardless of whatever the recursive calls returned. You need to change this.
bool ans1 = sol(arr,a,n,i+1,j);
a[j]=arr[i];
bool ans2 = sol(arr,a,n,i+1,j+1);if(ans1 || ans2) { return true; } return false;
This ensures that the results obtained from child recursive calls are utilised in the parent recursive calls and the final result returned is based on the result from the child recursive calls.
1 Like
thank you Sir.Ii have never felt more satisied with any of the other ta’s
it is still giving wrong answer
@sattyrajpoot39
A few more little changes required.
- Line No. 8 , run your loop till k<j only not k<=j .
- Line No. 35 , initialise your array a with values 0.
int a[n] = {0} .
This is to ensure that your code does not pick up any random values while computing. - Finally , change Line No. 12 if condition to if(sum==0 && j>0)
As we know that the NULL subset is always a subset of any array. The sum produced by the NULL subset would always be zero and hence if we were to consider that , our output would always be “Yes” . We are not considering it here in this problem. In the NULL subset case , the value of j would be zero and that would be the only case where it would be zero. So we can eliminate the possibility of counting NULL subset by checking whether j>0 in our conditions.
1 Like
thankyou sir for awesome explaination