TLG - Editorial

array
easy
editorial

#61

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


#62

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?


#63

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*=Si-Ti;
}     

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

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


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

}


#64

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

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


#65

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

}

}


#66

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.


#67

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

    }

}


#68

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

scanf("%d",&y*);

}

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

{

diff=0;

if(x*>y*)

{

diff=x*-y*;

if(diff>maxdiffx)

{

maxdiffx=diff;

}

}

else

{

diff=y*-x*;

if(diff>maxdiffy)

{

maxdiffy=diff;

}

}

}

if(maxdiffx>maxdiffy)

printf("1 %d
", maxdiffx);

else

printf("2 %d
", 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.


#69

Why is this wrong??
https://www.codechef.com/viewsolution/15808289

import java.util.Scanner;
class TLG
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int maxLead=0, leader=0;
for(int i=0;i<n;i++)
{
int p1 = scan.nextInt();
int p2 = scan.nextInt();
if(Math.abs(p1-p2)>maxLead)
{
maxLead = Math.abs(p1-p2);
if(p1>p2) leader=1;
else leader=2;
}
}
System.out.println(leader+" "+maxLead);
scan.close();
}
}


#70

import java.util.Scanner;
class Tlg
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,max,min,j=0,k=0;

	if(n>=1 && n<=10000)
	{
		int[] s=new int[n];
		int[] t=new int[n];
		int[] res=new int[n];
		for(i=0;i<n;i++)
		{
			max=sc.nextInt(); 
			min=sc.nextInt();
			if(max>=0 && max<=1000)
			{ s*=max; k++;}
		    if(min>=0 && min<=1000)
			{ t*=min; j++;}
			res*=s*-t*;
		}
		
		
		if(j==n && k==n)
		{
		    max=res[0];
		    min=res[0];
		    for(i=1;i<n;i++)
		    {
	    	    if(max<res*)
				  max=res*;  //30x1		
		    }
		    for(i=1;i<n;i++)
		    {
			    if(min>res*)
				  min=res*;  //-60
		    }
		    if(max<0)
			   max=max*(-1);  //max=30
		    if(min<0)
 			   min=min*(-1);  //min=60
		
		    if(max>min)
			   System.out.print("1" +" "+ max);
		    else
			   System.out.print("2" +" "+ min);
	    
		}
	}
}

}
i am getting wa(wrong answer)


#71

whats wrong in my code

import java.util.Scanner;

public class TheLeadGame
{
public static void main(String[] Sym)
{
Scanner sym = new Scanner(System.in);

	int round = sym.nextInt();
	int[] intarrr = new int[]{0,0};
	
	for (int i = 0; i < round; i++) 
	{
		int a = sym.nextInt();
		int b = sym.nextInt();
		
		int absVal = Math.abs(a-b);
		if(intarrr[1] < absVal) 
		{
			intarrr[1] = absVal ;
			intarrr[0] = (a > b) ? 1 : 2;
		}
	}
	System.out.println(intarrr[0] + " " + intarrr[1]);
	sym.close();
}

}


#72

I don’t think there is need of creating 4 arrays. I solved this answer using 4 variables only. Codechef shows wrong answer when I submit my code but when I run the code on my computer the answer that I get always correct. Below is the code that I wrote.

#include <iostream>
using namespace std;
int main()
{
short int t,p1,p2,m1=0,m2=0;
cin >> t;
while(t--)
{
cin >> p1 >> p2;
if(p1>p2 && (p1-p2)>m1)
m1 = p1-p2;
else if(p2>p1 && (p2-p1)>m2)
m2 = p2-p1;
}
if(m1>m2)
cout << 1 << " "<<m1<<endl;
else
cout << 2 << " "<<m2<<endl;
return 0;
}

#73

#include

using namespace std;

int main()
{
int R;
cin>>R;
int P1[R],P2[R],maximum=0,diff=0,W=0;
for(int a=0;a<R;a++)
{
cin>>P1[a]>>P2[a];
if(P1[a]>P2[a])
{
diff=P1[a]-P2[a];
if(diff>maximum)
{
W=1;maximum=diff;
}}
else{diff=P2[a]-P1[a]; if(diff>maximum){W=2;maximum=diff;}
}
}
cout<<W<<" "<<maximum<<endl;
return 0;
}

what is wrong with this code. The answer matched with several test data.


#74

I did that without using any array. what is problem with my code.

#include <iostream>
using namespace std;
int main()
{
int t,p1,p2,m1=0,m2=0;
cin >> t;
while(t--)
{
cin >> p1 >> p2;
if(p1>p2 && (p1-p2)>m1)
m1 = p1-p2;
else if(p2>p1 && (p2-p1)>m2)
m2 = p2-p1;
}
if(m1>m2)
cout << 1 << " "<<m1<<endl;
else
cout << 2 << " "<<m2<<endl;
return 0;
}

#75

what is wrong with my code. whenever i am submitting it , its showing wrong answer , but i have tried many test cases and all working fine . please reply . thanku…

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. /
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int player1[] = new int[n];
int player2[] = new int[n];
int rem1 = 0;
int rem2 = 0;
for (int i = 0; i < n; i++) {
player1
= sc.nextInt();
player2* = sc.nextInt();
}
for (int i = 0; i < n; i++) {
if (player1* >= player2*) {
int sub1 = player1* - player2*;
if (sub1 > rem1)
rem1 = sub1;
} else{
int sub2 = player2* - player1*;
if (sub2 > rem2)
rem2 = sub2;
}
}

	if (rem1 > rem2)
	{
	         int  w =1;
		System.out.println(w+" "+ rem1);
	}
       	else
       	{
	      int  w =2;
		System.out.println(w+" "+rem2);
       	}

}

}


#76

import java.util.Scanner;

class Players{

public void leaderBoard(int[] p1, int[] p2, int n)
{
	int j=1;
	for(int i=0; i<n; i++){
	 p1* -= p2*;
	}
	
	int max =p1[0];
	
	for(int i=0; i<n; i++){

// System.out.println("LeaderBoard is: "+p1*);
if(max<p1*)
{
max = p1*;
j++;
}

	}
	System.out.println();
	System.out.println(j +" "+ max);
}

}

class TheLeadGame {
public static void main(String[] args) //throws IOException
{

	Players  play = new Players();
	Scanner sc = new Scanner(System.in);
	
	int n= sc.nextInt();
	int p1[] =new int[n];
	int[] p2 = new int[n];

// System.out.println("Enter first player score ");
for(int i=0; i<n; i++)
{
p1* = sc.nextInt();

	}
	System.out.println();

// System.out.println("Enter second player score ");
for(int i =0; i<n; i++){
p2* = sc.nextInt();
}

	play.leaderBoard(p1, p2, n);
	

}

}


#77

function myFunc(arr1,arr2) {
for (let i = 0; i < 3; i++) {
let lead = [];
if (arr1* > arr2*) {
if (lead<(arr1* - arr2*)) {
lead = arr1* - arr2*;
return (lead + " P1 has lead");
}
}
else{
if (arr1* < arr2*) {
if (lead < (arr2* - arr1*)) {
lead = arr2* - arr1*;
return (lead + " P1 has lead");
}
}
}
}
return lead;

}
console.log(myFunc([8,9,6],[8,5,8]));

//–Whats Wrong with my Code–//
//–trying to run on console in JS–//


#78

Hey,here is my code, it is showing correct output in my laptop for all inputs but here it is showing wrong answer. can someone tell me why ?

n=int(input())
list1=[]
list2=[]
list3=[]
list4=[]
for i in range(n):
(a,b)=list(map(int,input().split()))
list1.append(a)
list2.append(b)
a=abs(a)
b=abs(b)
if(a>b):
c=(a-b)
d=1
list3.append©
list4.append(d)
else:
c=b-a
d=2
list3.append©
list4.append(d)
position=list3.index(max(list3))
print(list4[position],max(list3))


#79

what is wrong with my code?
#include
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;

    int p1[n],p2[n],lead[n],i,large1=0,largest,w;
    double mlead[n];
    for(i=0;i<n;i++)
    {
        //cout<<"p1"<<endl;
        cin>>p1*;
        //cout<<"p2"<<endl;
        cin>>p2*;
        lead*=p1*-p2*;
        mlead*=abs(lead*);



    }
        largest=mlead[0];
        for(i=1;i<n;i++)
        {
            if(largest<mlead*)
               {

                largest=mlead*;
                large1=i;
               }
        }
        //cout<<largest<<endl;

        //cout<<lead[large1];
    if(lead[large1]>0)
    {
        w=1;
        cout<<w<<" "<<largest;
    }
    else
    {
        w=2;
        cout<<w<<" "<<largest;
    }



return 0;

}


#80

n=int(input())
lead=[]
for i in range(n):
score=[int(x) for x in input().split()]
lead.append(score[0]-score[1])
if((n==1)):
if(score[0]>score[1]):
print(1,score[0]-score[1])
if(score[0]<score[1]):
print(2,score[1]-score[0])
player1=abs(max(lead))
player2=abs(min(lead))
if(player1>player2):
print(1,player1)
elif(player1<player2):
print(2,player2)

what is wrong with my code