TLG - Editorial

array
easy
editorial

#41

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.


#42

what’s wrong with this code
link to code thanks


#43

#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*;
    cin >> B *;
}

for( int i =0; i<N; i++)
{
if(A* - B* >0 && (A* - B*) > maxi){
maxi =(A* - B*);
W =1;
}
else if(B* - A* > maxi)
{
maxi =(B* - A*);
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.


#44

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

#45

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*>>p2*;
	  if(p1*>p2*)
	 {
		p1lead[j]=p1*-p2*;
		++j;;
	}
	else
	{
		p2lead[k]=p2*-p1*;
		++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;

}


#46

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;

}


#47

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;
}


#48

Can any one tell why my submission is wrong.
https://www.codechef.com/viewsolution/12824863.


#49

#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?


#50

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)


#51

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


#52

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.


#53

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*=(p1-p2);

p22*=(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.


#54

no use of array is required


#55

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


#56

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* = t.nextInt();
b* = 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* = a1* + a*;
		b1* = b1* + b*;
	}


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

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


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

	}

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

}

}


#57

#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*,&array2*);
    if(array1*>array2*)
    {
        if(array1*-array2*>lead)
        {
            lead=array1*-array2*;
            leader=1;
        }
    }
    else{
         if(array2*-array1*>lead)
        {
            lead=array2*-array1*;
            leader=2;
        }
    }
}
printf("%d %d",leader,lead);

return 0;
}

sir why it give wrong answer pledge help me


#58

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;
}


#59

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

}


#60

#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<<"

Enter 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<<"

Player 1 score";
cin>>player1[itr];
cout<<"
Player 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<<"

Player1 is winner with lead of “<<win;
else
cout<<”
Player2 is winner with lead of "<<win;

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

}