# why do i get wrong answer even when my code works for given input and other test cases?

my solution for problem COUPSYS is
#include<stdio.h>

int main()
{
int n,t;
scanf("%d",&t);
int max1,max2,max3,c1,c2,c3,i,j;
max1 =0,max2=0,max3=0,c1=0,c2=0,c3=0;
while(t–)
{
scanf("%d",&n);
int a[n][3];
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
if((ai==1) && a[i][2]>=max1)
{
if(a[i][2]==max1)
{
if(a[i][0]<c1)
{
c1=a[i][0];
max1=a[i][2];
}
else
continue;
}
else
{
c1=a[i][0];
max1=a[i][2];
}
}
else if((ai==2) && a[i][2]>=max2)
{
if(a[i][2]==max2)
{
if(a[i][0]<c2)
{
c2=a[i][0];
max2=a[i][2];
}
else
continue;
}
else
{
c2=a[i][0];
max2=a[i][2];
}
}
else if((ai==3) && a[i][2]>=max3)
{
if(a[i][2]==max3)
{
if(a[i][0]<c3)
{
c3=a[i][0];
max3=a[i][2];
}
else
continue;
}
else
{
c3=a[i][0];
max3=a[i][2];
}
}
}
printf("%d %d \n",max1,c1);
printf("%d %d \n",max2,c2);
printf("%d %d \n",max3,c3);
}
return 0;
}

Give link to your code and the problem, don’t just paste your code here, its hard to understand for anyone.

1 Like

And you’ve declared all variables outside the while loop. Hence for each test case their values are not initialized to zero which may be causing wrong answer…initialize them to zero inside the while loop

1 Like