TLG My code is giving correct answer in all the testcases i am putting but in codechef it is showing wrongs, please help to debug.

#include

using namespace std;

int main()
{
int n,*a,*b,diff1=0,diff2=0;

cin>>n;
a=new int[n];
b=new int[n];

int i=0;

for(i=0;i<n;i++)
{
    cin>>a[i]>>b[i];

    if(a[i]>=b[i])
    {
        if(diff1<=a[i]-b[i])
        diff1=a[i]-b[i];
 //       cout<<"diff1 "<<diff1<<endl;
    }else
    {
        if(diff2<=b[i]-a[i])
        diff2=b[i]-a[i];
 //       cout<<"diff2 "<<diff2<<endl;
    }
}

if(diff1>diff2)
cout<<1<<" "<<diff1;
else
cout<<2<<" "<<diff2;

return 0;

}

First of all, I guess you need to re-read the problem statement.
The catch/trick in the problem is that at each round previous scores are added as well, so lead is calculated for the scores and it is very much clearly depicted in the problem statement.

Let me explain you with the test case given :
Round Player 1 Player 2

  1       140          82
  2        89         134 
  3        90         110 
  4       112         106
  5        88          90 
so after round 1 player 1 scores 140 and player 2 scores 82.So lead is 140-82 i.e. 58 and leader is 1. In round 2 player 1 scores 89 and player 2 scores 134. So according to your solution, in this round player 2 is leader and lead equals to 134-89 = 45.

But actually till now the total scores are 229 and 216 for player one and two respectively.
So player 1 is still leading but his lead has been curtailed to just 13. so we need to consider this lead not the individual round difference.

Just include this and you will get accepted. Hope you got it.
PS : If you still have some questions or doubts feel free to ask and reply me @viaan on this thread itself, i’ll try my best to answer you.

2 Likes

@viaan, I have written a new code, after again reading the question …

#include

using namespace std;

int main()
{
int a1,a2,diff1=0,diff2=0,sum1=0,sum2=0,n,i=0;

cin>>n;

for(i=0;i<n;i++)
{
    cin>>a1>>a2;
    sum1+=a1;
    sum2+=a2;

    if(sum1-sum2>0)
    {
        if(diff1<sum1-sum2)
        {
            diff1=sum1-sum2;
        }
    }else
    {
        if(diff2<sum2-sum1)
        {
            diff2=sum2-sum1;
        }
    }
}


if(diff1>diff2)
cout<<1<<diff1;
else
cout<<2<<diff2;


return 0;

}

but here also the answer is coming wrong.

@rajat_121292

have a look at the output section
i guess you are missing the space between the leader and the lead.
cout<<1<<" "<< diff1 ORcout<<"1 "<< diff1
will do good.

Hope I was helpful enough and you learnt something from this problem.

Kudos.

1 Like

Thank you very much…

focus on overall lead score

Your output must consist of a single
line containing two integers W and L,
where W is 1 or 2 and indicates the
winner and L is the maximum lead
attained by the winner.

there L is maximum overall lead, but it needs to be calculated per round

@rajat_121292
No need to thank me.
It’s my pleasure.
We need to grow as Indian coding fraternity so we all are buddys here.
So If u are satisfied with the answers you can close the thread and chose the question has been answered.

1 Like

viaan can I talk with you personally about some problems of SPOJ, can you give me your email id or fb id., only if you say, I am not forcing…

Can You please help me with this code.It runs fine and passed all the custom input test cases
But on submission,the answer is being marked as wrong.

`#include <stdio.h>

int main(void) {
// your code goes here
int n;
int max=0;
int ac=0,bc=0;

scanf("%d",&n);
int lead,maxB=0,maxA=0;
 int a1=0,b1=0;
while(n){
   
    int a,b;
    scanf("%d %d",&a,&b);
    a1+=a;b1+=b;
 
         lead=a1-b1;
  
  
    
    if(lead>0 && lead>maxA){
        maxA=lead;

    }
    else if(lead<0 && -lead>maxA) 
    {
          maxA=-lead;
          
    }
    n-=1;
}
if(maxA>0){
    printf("1 %d",maxA);
}
else{
      printf("2 %d",-maxA);
}
return 0;

}

`

check this code output is correct but status is showing wrong. please have a look.

#include <stdio.h>
int main(){
int t,w;
int m=0;
scanf("%d",&t);
int a[t],b[t],l[t];
for(int i=0;i<t;i++)
{
scanf("%d%d",&a[i],&b[i]);
}
for(int i=0;i<t;i++)
{
if(a[i]>b[i])
{
l[i]=a[i]-b[i];
if(l[i]>=m){
m=l[i];
w=1;
}

          }
      else(b[i]>a[i]);
          {
          l[i]=b[i]-a[i];
          if(l[i]>=m){
             m=l[i];
             w=2;
             }
          } 
      } 
    printf("%d %d\n",w,m);
}

@viaan please check this and tell me whats wrong.
l1,l2 are the lists for all leads of player 1 and 2 respectively.
score1 is cumulative score of player 1
score2 is cumulative score of player 2

t=int(input())
l1=[]
l2=[]
score1=0
score2=0
for i in range(t):
a,b=map(int,input().split())
score1=score1+a
score2=score2+b
if(score1>score2):
l1.append(score1-score2)
else:
l2.append(score2-score1)
if(max(l1)>max(l2)):
winner=1
print(winner,max(l1))
else:
winner=2
print(winner,max(l2))