TLG - Editorial

rounds=input()
p1_score=[]
p2_score=[]
difference_array=[]
for i in xrange(0,rounds):
a, b = map(int, raw_input().split())
p1_score.append(a)
p2_score.append(b)
for t in range(0,rounds):
difference=p1_score[t]-p2_score[t]
difference_array.append(difference)
difference_array.sort()
x1=abs(difference_array[0])
x2=abs(difference_array[rounds-1])
if x1>x2:
print “2”, x1
elif x2>x1:
print “1”, x2
else:
pass

This is the code I wrote in Python 2.7. Please tell me what’s wrong with this. Test case works fine with it.

what’s wrong with this code
link to code thanks

#include
#include
using namespace std;

int main()
{
int N;
cin >> N;

int B[N];
int A[N];

int lead[N];

int W=0;
long long maxi =0;

for(int i=0; i<N; i++)
{
    cin >> A[i];
    cin >> B [i];
}

for( int i =0; i<N; i++)
{
if(A[i] - B[i] >0 && (A[i] - B[i]) > maxi){
maxi =(A[i] - B[i]);
W =1;
}
else if(B[i] - A[i] > maxi)
{
maxi =(B[i] - A[i]);
W =2;
}
}

cout << W << " " << maxi;
}

Can anyone please help me, whats wrong in my code. It works fine in my pc but when i submit it shows wrong answer.

What’s wrong with my code please

import sys
count = int(input())
p1_leads = []
p2_leads = []

while count != 0:
    x, y = [int(item) for item in input().split()]
    if (x != y):
        if (x > y):
            p1_leads.append(x-y)
        else:
            p2_leads.append(y-x)
    else:
        sys.exit(1)
    count -=1

if max(p1_leads) > max(p2_leads):
    print(1, max(p1_leads))
else:
    print(2, max(p2_leads))

where is the problem in following code. it gives right answer in my pc but wrong answer shows in codechef compiler. please help!

#include<iostream>
using namespace std;
main()
 {
   int n;
   cin>>n;
   int p1[n],p2[n],p1lead[n],p2lead[n],j=0,k=0;
   for(int i=0;i<n;i++)
        {
	  cin>>p1[i]>>p2[i];
	  if(p1[i]>p2[i])
	 {
		p1lead[j]=p1[i]-p2[i];
		++j;;
	}
	else
	{
		p2lead[k]=p2[i]-p1[i];
		++k;
	}
	
}
int max1,max2;
max1=p1lead[j-1];
for(int p=j-2;p>=0;p--)
{
	if(p1lead[p]>max1)
	{
		max1=p1lead[p];
		}	
}
max2=p2lead[k-1];
for(int q=k-2;q>=0;q--)
{
	if(p1lead[q]>max1)
	{
		max2=p1lead[q];
		}	
}
if(max1>max2)
{
	cout<<"1"<<" "<<max1;
}
else
{
	cout<<"2 "<<max2;
}
return 0;

}

WHAT IS WRONG WITH THIS???

#include<stdio.h>
int max=0,i=0;
int main()
{

int n;
scanf("%d",&n);


while(n!=0)
{
	
	int x,y;
	scanf("%d %d",&x,&y);
	
	if((x-y)>max)
	{
		max=(x-y);
		i=1;
	}
	if((y-x)>max)
	{
		
		max=(y-x);
		i=2;
	}
	
	
	n=n-1;
}


printf("%d %d",i,max);

return 0;

}

WHATS WRONG WITH MY ANSWER.

#include <stdio.h>
#include <stdlib.h>
#define PLAYER1 1
#define PLAYER2 2
//this program is to count scores of two in a game for N round and declaring winner.
int main()
{
int n,si,ti;
int L1=0,L2=0;
scanf("%d",&n);
while(n–){
scanf("%d %d",&si,&ti);
if( (si-ti) > L1 ? (L1=si-ti):0){
}
else if ((ti-si)>L2 ? (L2=ti-si) : 0);
}
if(L1>L2) printf("%d %d",PLAYER1,L1);
else printf("%d %d",PLAYER2,L2);
return 0;
}

Can any one tell why my submission is wrong.
CodeChef: Practical coding for everyone.

#include

using namespace std ;

int main(){
int i=1,p1,p2,n,lead=0,maxlead1=0,maxlead2=0;
cin >> n;
while(i<=n){
cin >> p1 >> p2 ;
lead = lead + p1-p2 ;
if(p1-p2>maxlead1){maxlead1=p1-p2;}
if(p2-p1>maxlead2){maxlead2= p2-p1;}
i++ ;
}
if(lead>0){
cout << “1” << " " << maxlead1 << endl;
}
else{
cout << “2” << " " << maxlead2 << endl;
}
return 0;
}

what is wrong here?

lead = lead + p1-p2

This seems wrong to me since lead in one round is independent of other/previous rounds. MEaning, if 1 had lead of 50 in round one, and next round scores are Player1- 250, player2 -260 then elad of 2 will be 10. But your statement will do lead = 50(lead) -10 (p1-p2)

Greetings!
May I ask for an optimal test case for this problem?
all the test cases that I use are confirmed but I am still getting wrong answer on submission
and I need to check if I am doing wrong and where.
Best Regards.
Robert.

Here is my code:
https://www.codechef.com/viewsolution/12893871

https://discuss.codechef.com/questions/58753/tlg-editorial/83486
Hi, the problem with your code is that it only gives the output result of the last scores input in it. You need to give the result from all of the rounds’ scores input.

1 Like

import java.util.*;

class {

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

int p11[]=new int[t];

int p22[]=new int[t];

if(t<=10000)

{

for(int i=0;i<t;i++)

{

int p1=sc.nextInt();

int p2=sc.nextInt();

p11[i]=(p1-p2);

p22[i]=(p2-p1);

}}

Arrays.sort(p11);

Arrays.sort(p22);

if(p11[t-1]>p22[t-1])

{
int k=1;

System.out.print(k+" "+p11[t-1]);

}

else{

int l=2;

System.out.print(l+" "+p22[t-1]);}

}
}

This is my code. its working fine with the sample test but is giving it wrong answer.

no use of array is required

#include
#include
//#include
#include<math.h>
#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
vector< pair<int,int> > v;
for(int i=0;i<t;++i)
{
int si,ti,winner,lead;
cin>>si>>ti;
winner=(si>ti)?(1):(2);
lead=abs(si-ti);
v.push_back(make_pair(lead,winner));
}
sort(v.begin(),v.end(),greater< pair<int,int> >());
cout<<v[0].second<<" "<<v[0].first<<endl;
v.clear();
return 0;
}

Code Works fine. why codechef showing Wrong Answer??

import java.util.;
import java.io.
;
class lead{
public static void main(String args[]){
Scanner t = new Scanner(System.in);
int n = t.nextInt();//no. of rounds
int i, winner=0, max;
int a[] =new int[n];
int b[] =new int[n];
int c[] = new int[n];
for(i=0;i<n;i++){
a[i] = t.nextInt();
b[i] = t.nextInt();
}

	int a1[] =new int[n];
	a1[0] = 0;
	int b1[] =new int[n];
	b1[0] = 0;


	for(i=0;i<n;i++){
		a1[i] = a1[i] + a[i];
		b1[i] = b1[i] + b[i];
	}


		for(i=0;i<n;i++)
		{
		if(a1[i]>b1[i]){
			c[i] = a1[i]-b1[i];
		}
		else{
			c[i] = b1[i]-a1[i];
		}

	}
	max = c[0];
	for(i=0;i<n;i++)
		if(c[i]>max)
			max = c[i];


			for(i=0;i<n;i++)
		{
		if(a1[i]>b1[i]){
			winner = 1;
			if(c[i]==max)
				break;
		}
		else{
			winner = 2;
			if(c[i]==max)
				break;
		}

	}

	System.out.print(winner+ " ");
	System.out.println(max);

}

}

#include<stdio.h>
int main()
{
int n,i,leader,lead=0;
scanf("%d",&n);
int array1[n],array2[n];
for(i=0;i<n;i++)
{

    scanf("%d%d",&array1[i],&array2[i]);
    if(array1[i]>array2[i])
    {
        if(array1[i]-array2[i]>lead)
        {
            lead=array1[i]-array2[i];
            leader=1;
        }
    }
    else{
         if(array2[i]-array1[i]>lead)
        {
            lead=array2[i]-array1[i];
            leader=2;
        }
    }
}
printf("%d %d",leader,lead);

return 0;
}

sir why it give wrong answer pledge help me

What’s wrong with this code ?

#include<stdio.h>
#include<math.h>
int main()
{
int i,n, a, b, max=-1, index ;
scanf("%d",&n);
for( i=0;i<n;++i)
{
scanf("%d %d",&a,&b);
if(fabs(a-b)>max)
{
max=fabs(a-b);
if(a>b)
index=1;
else
index=2;
}
}
printf("%d %d",index, max);
return 0;
}

import java.io.;
import java.util.
;
import java.text.;
import java.math.
;
import java.util.regex.*;

class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int t = scan.nextInt();
    int player=0,max;
    int almax=0,playerf=0;
    while(t-->0){
        int count=0;
        int a = scan.nextInt();
        int b = scan.nextInt();
        if(a>b){
            max=a-b;
            player=1;
        }
        else{
            max=b-a;
            player=2;
        }
     //finding maximum
        if(max>=almax){
            almax=max;
            playerf=player;
        }
    }
    System.out.println(playerf+" "+almax);
}

}

1 Like

#include

using namespace std;

int modulusNum(int num)
{
int temp = 0;
if(num>0)
temp=num;
else
temp=-num;
return temp;
}

void main()
{
int rnds = 0, s = 0, temp = 0, win = 0;

cout<<"\nEnter the rounds:";
cin>>rnds;

/* arrays*/
int* player1 = new int[rnds];
int* player2 = new int[rnds];
int*	lead = new int[rnds];
int* modLead = new int[rnds];

for(int itr=0; itr<rnds; itr++)
{
	cout<<"\nPlayer 1 score";
	cin>>player1[itr];
	cout<<"\nPlayer 2 score";
	cin>>player2[itr];

	s=s+player1[itr] - player2[itr];
	lead[itr] = s;
	modLead[itr] = modulusNum(lead[itr]); 
}

/* find the max element*/
for(int itr=0; itr<rnds; itr++)
{
	if(modLead[itr]>temp)
	{
		temp = modLead[itr];
		win = lead[itr];
	}
}

if(win > 0)
	cout<<"\n Player1 is winner with lead of "<<win;
else
	cout<<"\n Player2 is winner with lead of "<<win;

/* Clean up */
free(player1);
free(player2);
free(lead);
free(modLead);

}