getting a wrong answer

question is

my sol is
http://www.codechef.com/viewsolution/5150997

dont know why i am getting a wrong answer…already checked all test cases given…

4 Likes

Oh! It gives you a wrong answer when m=0 but n!=0 . Say t=5, m=5, n=0 , 1, 2, 3, 4, 5. The answer should be No as you cannot make zero with these bank notes. Your code outputs yes though.
Solution :- Add an if condition, simple!

2 Likes

Hi dude, checking for the sample input test cases doesn’t guarantee that your solution will be right(AC). Pranjal has already pointed out the test case for which your code fails. Also, while taking inputs you are printing an extra line for each input which isn’t required.

2 Likes

#include<stdio.h>

void sort(int a[],int n)
{

int i,j,temp;
for(i=0;i<n;i++)
{
	for(j=i+1;j<n;j++)
	{
		if(a[j]<a[i])
		{
			temp=a[j];
			a[j]=a[i];
			a[i]=temp;
		}
	}
}

}

int main()
{

int i,j,k,s1,q,s2,t,m,n,a[25];
scanf("%d",&t);
while(t--)
{
	s1=0;k=0;s2=0;
	scanf("%d%d",&n,&m);
	if((n==0)||(m==0))
	{
		printf("No\n");
	    continue;
	}
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	sort(a,n);
	//for(i=0;i<n;i++)
	//printf("%d\t",a[i]);
	//printf("\n\n\n\n");
	for(i=0;i<n;i++)
	{
		if(a[i]==m)
		{
			s1=1;
			break;
		}
		if(a[i]>m)
		{
			q=i-1;
			break;
		}
		if(a[i]<m)
		s2+=1;
	}
	//printf("%d\n",q);
	if(s1==1)
	{
		printf("Yes\n");
		break;
	}
	if(s2==n)
	q=n-1;
	//printf("%da\n\n",q);
	if(s1==0)
	{
		for(i=q;i>0;i--)           
		{ k=0;
		    k+=a[i];
			for(j=i-1;j>=0;j--)     
			{
				k+=a[j];
				if(k>m)
				k-=a[j];
				if(k==m)
				break;
			}
			if(k==m)
			break;
			
		}
	}
		if(k==m)
		printf("Yes\n");
		else
		printf("No\n");
}
return(0);

}

i made some changes…but still nt working…

3 Likes

Fails for this test case, the output for the 2nd case should be No → (Link) wz91x1 - Online C Compiler & Debugging Tool - Ideone.com

1 Like

but in my compiler in dev c++ the answer for 2nd case…is coming NO…
i don’t know how this is possible that answer are not matching…for this case…

3 Likes

i have checked it twice…its coming no

Enter the same test cases(in the same order) and I am pretty sure you will get the above results if the code is same.

1 Like

again coming NO…just entered the same test cases…in same order

3 Likes

http://www.codechef.com/viewsolution/5153711

here is the last code uploaded by me…i am checking ur cases in this one…

3 Likes

Again a test case for which your code fails :slight_smile: L0aDJ1 - Online C Compiler & Debugging Tool - Ideone.com

1 Like

okay…i will get through the algo again…
thank u for ur help…

1 Like