Paying up,MARCHA 1 getting wrong answer please help

#include<bits/stdc++.h>
using namespace std;
bool recur(int a[],int sum,int i,int j)
{
if(sum==a[i])
return true;
else if(sum<a[i])
return false;
else if(i==j&&sum!=0)
return false;
else
return(recur(a,sum-a[i],i+1,j)||recur(a,sum,i+1,j));
}
int main()
{
int tc,z,x,k,count,l;
cin>>tc;
while(tc–)
{ count=0,l=1;
cin>>z>>x;
int b[z];
for(int j=1;j<=z;++j)
{
cin>>k;
if(k<=x)
{
b[l++]=k;
++count;
}
}

        bool ans=recur(b,x,1,count);
        if(ans)
        cout<<"Yes\n";
        else
        cout<<"No\n";
    }
    return 0;
}

Can you please give a link of your code after running it on ideone.com. It’s very difficult for people to understand the code if it’s written in this way.

There, fixed the formatting dear :slight_smile:

I hope its clear to you now :slight_smile:

@vijju123 The case in which your complier gave TLE is working fine HERE

3 Likes

Thats strange! The codechef compiler is still giving me TLE. Thanks for informing!

I tried 2 test cases on your code when the required sum to be found is 0.Here.Answer for both the cases should be “Yes” but for one case its yes while for other its no.Hope you can debug it now.Here is the code at geeks for geeks.You can check your recursion from here and correct your solution.

2 Likes

Nice!!

But one question, is m=0 allowed? I find input section quite lacking here, it doesn’t mention the upper and lower limit of constraints. Especially of m.

This is subset sum problem and in this sum=0 is allowed.I am not sure for this particular problem if its allowed here or not but generally its allowed.

1 Like

Noted. Thanks for information!! :slight_smile:

1 Like