my program gives correct output but it still shows wrong answer
#include
using namespace std;
int main()
{
int a[10000][2],lead,n,i,diff;
int flag1=0,flag2=1;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i][0]>>a[i][1];
}
lead=a[0][0]-a[0][1];
for(i=0;i<n;i++)
{
if(a[i][0]>a[i][1])
{
diff=a[i][0]-a[i][1];
flag1=0;
}
else
{
diff=a[i][1]-a[i][0];
flag1=1;
}
if(diff>lead)
{
lead=diff;
flag2=flag1;
}
}
cout<<flag2+1<<" "<<lead;
return 0;
}
Your approach was right but you found the difference between scores in each round and then compared it to the lead.
However, the question asked you to find the difference between the sum of scores until that round and then compare it to lead. Update the lead if the difference was more than the lead.
suppose there are two rounds with scores as:
13 4
12 8
What your solution does:
when i=0,
diff = 13-4=9
lead = 9
when i=1,
diff = 12-8 = 4
since 4<9 , lead remains 9
The real solution :
i=0
diff = 13-4=9
lead = 9
i=1
diff = (12+13)-(4+8) = 13 , here I have taken the sum of scores till round 2 so player 1 cumulative score = 12+13 =25, after that I have found the difference between cumulative scores and compared it to the lead.
since 13>9, lead= 13
I modified your solution to match the question requirements and then submitted it.