TLG - Editorial

#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);

}

t=int(input())
x=0
y=0
while t>0:
a, b = map(int,input().split())
if x<a-b:
x = a-b
elif y<b-a:
y=b-a
if x>y:
leader=1
elif y>x:
leader=2
t=t-1
if x>y:
print(leader," “,x)
elif y>x:
print(leader,” ",y)
What is wrong in this code?? I’m getting wrong answer whenever I submit this whereas I’ve checked this with all the test samples along with many of my own samples

t=int(input())
winner=1;
max=0;
for i in range(t):
a,b=input().split(" ");
diff=int(a)-int(b)
if(abs(diff)>=max):
max=abs(diff);
if(diff>0):
winner=1;
elif(diff<0):
winner=2;
print(winner,max)

what is worng?

what is wrong with this code? It it working fine on ideone and displaying the desired output.

int* getArray(int NOE)
{
return (int*)malloc(sizeof(int)*NOE);
}

int main()
{
int N, Si,Ti;
int i;
cin>>N;
int* A;
A=getArray(N);

for(i=0; i<N; i++)
{
    cin>>Si;
    //cout<<" ";
    cin>>Ti;
    A[i]=Si-Ti;
}     

int L=abs(A[0]);
int W;

for(i=1; i<N; i++)
{
   if(abs(A[i]) > L)
        L=A[i];
}


if(L>0)
    W=1;
else
    W=2;
    
cout<<W<<" "<<abs(L);
return 0;

}

What’s wrong in my code?
It’s working locally.

https://www.codechef.com/viewsolution/14663590

What’s wrong in my code? It’s working fine locally.Can anyone please explain?

package Beginner;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class The_Lead_Game {

public static void main(String[] args) {
	
	int t,a,b,c,c1;
	Scanner sc=new Scanner(System.in);
	t=sc.nextInt();
	List<Integer> list=new ArrayList<>();
	List<Integer> list1=new ArrayList<>();
	
	while(t-->0)
	{
		a=sc.nextInt();
		b=sc.nextInt();
		
		if(a>b)
		{
			c=a-b;
			list.add(c);
		}
		else
		{
			c1=b-a;
			list1.add(c1);
		}
		
		
		
		//System.out.println(c);		 
	}
	Collections.sort(list, Collections.reverseOrder());
	Collections.sort(list1, Collections.reverseOrder());
	//Collections.sort(list);
	//Collections.sort(list1);
	
	if(list.isEmpty())
	{
		System.out.println("2");
		System.out.println(list1.get(0));
		
	}
	else if(list1.isEmpty())
	{
		System.out.println("1");
		System.out.println(list.get(0));
		
	}
	else
	{
	
		if(list.get(0)>list1.get(0))
		{
			System.out.println("1");
			System.out.println(list.get(0));
			
		}
		else if(list1.get(0)>list.get(0))
		{
			System.out.println("2");
			System.out.println(list1.get(0));
		}
	
	}

}

}

Once all the rounds are over the player who had the maximum lead at the end of any round in the game is declared the winner.

I think it means any game out of all having highest lead decide the winner right ?
but that is not the case being accepted here.
if other player catches up the lead in 2 or 3 games like Highest lead is say 50 and other person manages lead of 30 in 2 games, he/she is winner.

This is so confusing.

What is the problem with my code?

import java.util.Scanner;

/**
*

  • @author vikaljai
    */
    public class Main {
    public static void main(String args[]){
    int player, maxPlayer=0,diff, maxDiff=0,p1,p2;
    Scanner sc = new Scanner(System.in);

     int loop = sc.nextInt();
     while(loop>0){
         p1 = sc.nextInt();
         p2 = sc.nextInt();
         diff = java.lang.Math.abs(p2-p1);
         if(diff>maxDiff){
         maxDiff = diff;
         maxPlayer = (p1>p2)?1:2;
         }
         loop--;
     }
     System.out.println(maxPlayer+" "+maxDiff);
    

    }

}

#include<stdio.h>

int main()

{

int x[10000],y[10000], n, i, diff, maxdiffx=0, maxdiffy=0;

scanf("%d",&n);

for(i=0; i<n; i++)

{

scanf("%d",&x[i]);

scanf("%d",&y[i]);

}

for(i=0; i<n; i++)

{

diff=0;

if(x[i]>y[i])

{

diff=x[i]-y[i];

if(diff>maxdiffx)

{

maxdiffx=diff;

}

}

else

{

diff=y[i]-x[i];

if(diff>maxdiffy)

{

maxdiffy=diff;

}

}

}

if(maxdiffx>maxdiffy)

printf(“1 %d\n”, maxdiffx);

else

printf(“2 %d\n”, maxdiffy);

return 0;

}

What’s gone wrong with this code?? Runs perfectly in codeshef’s practice IDE but on question’s IDE show wrong answer.