Following is the solution which I coded for the Lead Game (CodeChef: Practical coding for everyone):-
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int w;
int l = 0;
int s, t;
while(n--)
{
cin >> s >> t;
if(abs(s-t) > l)
{
if(s > t)
{
w = 1;
l = s-t;
}
else
{
w = 2;
l = t-s;
}
}
}
cout << w << " " << l << "\n";
return 0;
}
It works perfectly fine for the sample test case and for some other test cases but gives WA on submitting. Going through the AC submissions for the problem I found the following code:-
#include <iostream>
using namespace std;
int main()
{
int t,n,m,s=0,max1=0,max2=0,k,sum1=0,sum2=0;
cin >> t; // Enter Test Case
while(t--)
{
cin >> n >> m; // Enter Player1 score and Player2 score
sum1 = sum1 + n; // Goes on adding score of Player1
sum2 = sum2 + m; // Goes on adding score of Player2
if(sum1>sum2)
max1=sum1-sum2; // Player1 score is greater
if(sum1<sum2)
max2=sum2-sum1; // Player2 score is greater
if(max1>s)
{
s=max1; // If player1 score difference is greater set s=max1
k=1;
}
if(max2>s)
{
s=max2; //If player2 score difference is greater set s=max1
k=2;
}
}
cout << k <<" "<< s << endl;
}
For the following input:-
8
140 82
89 134
90 110
112 106
88 90
80 160
24 68
100 40
The above code gives the following output:-
2 127
Whereas my code gives the following output:-
2 80
And from the input one can clearly see that Player 2 is the winner with a lead of 80, then where is this 127 coming from? And then why am I getting WA when the AC solution gives an incorrect output? Please help.
Note:- I am not at all trying to insult the user who posted the above solution. I simply have a doubt.