TLG - Editorial

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.

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

Search the forums once. Intense profit :3

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[i]=max; k++;}
		    if(min>=0 && min<=1000)
			{ t[i]=min; j++;}
			res[i]=s[i]-t[i];
		}
		
		
		if(j==n && k==n)
		{
		    max=res[0];
		    min=res[0];
		    for(i=1;i<n;i++)
		    {
	    	    if(max<res[i])
				  max=res[i];  //30x1		
		    }
		    for(i=1;i<n;i++)
		    {
			    if(min>res[i])
				  min=res[i];  //-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)

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

}

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

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

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

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[i] = sc.nextInt();
player2[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
if (player1[i] >= player2[i]) {
int sub1 = player1[i] - player2[i];
if (sub1 > rem1)
rem1 = sub1;
} else{
int sub2 = player2[i] - player1[i];
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);
       	}

}

}

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[i] -= p2[i];
	}
	
	int max =p1[0];
	
	for(int i=0; i<n; i++){

// System.out.println("LeaderBoard is: "+p1[i]);
if(max<p1[i])
{
max = p1[i];
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[i] = sc.nextInt();

	}
	System.out.println();

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

	play.leaderBoard(p1, p2, n);
	

}

}