TLG: WHY IS IT GIVING WRONG ANSWER?

#include
#include<stdio.h>
#include
using namespace std;
int main()
{
int n,s[10000],t[10000],i,temp[10000],ans,k=0;
scanf("%d",&n);
ans=temp[0];
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
scanf("%d",&t[i]);
temp[i]=abs(s[i]-t[i]);

		}
		for(i=0;i<n;i++)
		{
			if(temp[i]>ans)
			{
				ans=temp[i];
				k=i;
			}
		}
		if(s[k]>t[k])
		printf("1  ");
		else
		printf("2  ");
	    printf("%d",ans);
			return 0;
	}

I think the problem is with this line:

 `  ans=temp[0];`

At this point ‘temp’ is not initialised, so it contains garbage value (any value). This may be set to ‘0’ on your computer, but is not guaranteed to be so in case of Codechef judge.

2 Likes

i tried temp[0] =0; but still it’s showing wrong anwer

Setting temp[0]=0 will not do any good. You should initialize ans to 0.

In the last but one line use printf("%d\n",ans);

1 Like

I think it won’t matter, since there is only one line in the answer. If there had been a number of test cases in each input test file, each answer should have been printed in a newline to make them distinguishable for the computer.

//