compilation error:Paying Up

#include<stdio.h>
int main()
{
int t,i,j,x,y,c=0,b[100],a[100],n,m,f;
scanf("%d",&t);
while(t–)
{
b[100]={-1};
f=0;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
x=y=b[0]=0;
for(i=0;i<n;i++)
{
for(j=0;j<=y;j++)
{
x++;
b[x]=a[i]+b[j];

        }
        y=x;
        c+=y;
    }
    i=0;
   while(b[i]!=-1)
   {
       if(b[i]==m)
       {
           f=1;
           break;
       }
       i++;
   }
   if(f==1)
        printf("Yes\n");
   else
        printf("No\n");
}
return 0;

}
You may also view the solution at the link below: http://www.codechef.com/viewsolution/6068032
Thanx for your support

@ma14ab03_1995

One reason of compilation error is b[100]={-1}; This syntax is wrong. If this is corrected then comes Run-time error (even for the sample cases). So you need to implement your size and approach differently for this problem. Feel free to ask if you need more help.

1 Like

What’s wrong in this approach??
array ‘b’ contains all the possible values that ‘m’ can have…then what’s wrong in this approach??

hey b[100]={-1} will assign -1 to only b[0] rest all b[1],b[2]…b[n] will be still 0…if you want to initialize whole array with -1 use fill_n() method…syntax will be fill_n(b,n,-1);

Yes…i solved the problem of initializing the array with -1, but now it is giving runtime error…can u help me solve it.I have taken the appropriate size of the array…as it was mentioned.You can look over to my code:
http://www.codechef.com/viewsolution/6069876
Thanks for your support

Have a look at this code. It is the accepted version of your code.Now let me explain the changes i had to make:

If there are 20 elements in a set, then the number of subsets if 2^20 which is close to 1050000. So that means, the size of your b array will br 1050000. The Runtime error you were getting was because of this reason only.
If you still have doubts, don’t hesitate to ask.

1 Like

Thanks. It worked!!

my pleasure!!