TLG - Editorial

PROBLEM LINK:

Practice

Author: ADMIN

Editorialist: SUSHANT AGARWAL

DIFFICULTY:

EASY

PREREQUISITES:

Basic looping,Arrays

PROBLEM:

At the end of each round the leader and her current lead are calculated. 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.

EXPLANATION:

Create two arrays(Player1-Stores the scores of player 1 in all the rounds)[a1,a2,a3…an] and (Player2-Stores the scores of player 2 in all the rounds)[b1,b2,b3…bn].

Create a third array “Lead” such that the i’th element of Lead is ((a1+a2…+ai) - (b1+b2…bi)).
Create a fourth array “modulus lead” such that the i’th element of this array is the modulus of the i’th element of “Lead”.

Find the maximum element of “modulus lead”.This is the maximum lead attained by the winner.If the element in the corresponding position of “lead” is positive then player 1 is the winner,otherwise player 2 is.

EDITORIALIST’S SOLUTION:

Editorialist’s solution can be found here.

2 Likes

I don’t think there is need of four different arrays, this thing can be done using just three variables

19 Likes

I think my solution is short and indented.

http://www.codechef.com/viewsolution/5886009

1 Like

what is wrong with my code??

#include<stdio.h>
int main()
{
long n,s1,s2,max,i,d,m;
scanf("%ld",&n);
max=0;
for(i=1;i<=n;i++)
{
scanf("%ld%ld",&s1,&s2);
if(s1>s2)
{
d=s1-s2;
if(d>max)
{
max=d;
m=1;
}
}
else
{
d=s2-s1;
if(d>max)
{
max=d;
m=2;
}
}

   }
   printf("%ld %ld\n",m,max);
   return 0;

}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

class LeadGame {

public static void main(String args[]){
	
	int num = 0;
	Scanner sc = null;

	Map<Integer, Integer> mp = null;
	
	try{
		sc = new Scanner(System.in);
		num = sc.nextInt();
		//sc.nextLine();
		mp = new HashMap<Integer, Integer>();
		for(int i = 0; i<num ; i++  ){

			//String[] s = sc.nextLine().split(" ");
			
			int pl1 = sc.nextInt(); //Integer.parseInt(s[0]);
			int pl2 = sc.nextInt();  //Integer.parseInt(s[1]);	
			
			mp.put(Math.abs(pl1-pl2), pl1>pl2 ? 1 : 2);	
		}
		findWinner(mp);
	}
	catch(Exception e){
		e.printStackTrace();
	}
}


private static void findWinner(Map<Integer, Integer> mp){
	
	Set<Integer> key = null;
	Iterator<Integer> itr = null;
	int large = -1;
	try{
		if(mp!=null)
			key = mp.keySet();
		itr = key.iterator();
		while(itr.hasNext()){
			int lead = itr.next();
			if(lead > large)
				large = lead;
		}
		System.out.println(mp.get(large)+" "+large);
	}
	catch(Exception e){
		e.printStackTrace();
	}
	
}

}

#include
#include<stdio.h>

using namespace std;

int main() {
int num,a,b,lead=0,i,name;
scanf("%d",&num);

	for(i=0;i<num;i++){
		scanf("%d",&a);
		scanf("%d",&b);
		if(a>b){
			if(lead<(a-b)){
				name=1;
				lead=a-b;
			}
			
		}
		else{
			if(lead<(b-a)){
				name=2;
				lead=b-a;
			}
		}
		
		
	}
	printf("%d %d",name,lead);

return 0;

}

I am getting a wrong answer dont know why. Please help.

2 Likes

#include<stdio.h>
main()
{

int t;
int lead[2]={0};
scanf("%d",&t);
while(t--)
{

	int a,b;
	scanf("%d%d",&a,&b);

	if(a>b  && (a-b)>lead[0])
	{
		lead[0]=a-b;
	}
	if(a<b && (b-a)>lead[1])
	{
		lead[1]=b-a;
	}


}

if(lead[0]>lead[1])
	printf("1 %d",lead[0]);
else 
	printf("2 %d",lead[1]);

return 0;

}

I dont understand whats wrong in it …plz help

What’s wrong with the following code:

#include
using namespace std;

int main()
{
int totalRounds, lead, maxLead1 = 0, maxLead2 = 0;
cin >> totalRounds;

int **stats = new int*[totalRounds];

for (int i = 0; i < totalRounds; i++)
	stats[i] = new int[totalRounds];

for (int i = 0; i < totalRounds; i++)
	cin >> stats[0][i] >> stats[1][i];

for (int i = 0; i < totalRounds; i++)
{
	if (stats[0][i] > stats[1][i])
	{
		lead = stats[0][i] - stats[1][i];
		if (lead > maxLead1)
			maxLead1 = lead;
	}

	else
	{
		lead = stats[1][i] - stats[0][i];
		if (lead > maxLead2)
			maxLead2 = lead;
	}
}

if (maxLead1 >= maxLead2)
	cout << "1 " << maxLead1;

else
	cout << "2 " << maxLead2;

for (int i = 0; i < totalRounds; i++)
	delete[] stats[i];

delete[] stats;
return 0;

}

I use a 2d array to store the data of the score of the two players as below:
[0][i] (1st Player) [1][i] (2nd Player)
[0][0] [1][0] <-- 1st Round
[0][1] [1][1] <-- 2nd Round
… … <-- … Round
[0][i - 1] [1][i - 1] <-- ith Round

So I was just trying out the pair implementation.Could someone figure out why is it a wrong answer?
https://www.codechef.com/viewsolution/8665285

Simply because I used pairs?

My Code is working absolutely fine with all the test cases and other custom inputs also but the moment i hit submit,it shows wrong answer.Can anyone help figure out the problem. Heres myn code. CodeChef: Practical coding for everyone

1 Like

@shubh87

Try with:

3

20 100

20 100

1000 0

The result should be player 1 with (1000+20+20)-(100+100)=840 lead points.

Your program gives player 2 with 1000 lead points.

2 Likes

@msrshahrukh @anantsaksena89

Try with:

3

20 100

20 100

1000 0

The result should be player 1 with (1000+20+20)-(100+100)=840 lead points.

Your program gives player 1 with 1000 lead points.

2 Likes

whats wrong with my code :the codechef compiler says wrong ans

#include<stdio.h>
int main()
{
int a,b,i,lead1=0,lead2=0;
scanf("%d",&i);
while(i>0)
{
scanf("%d%d",&a,&b);

  if((a-b)>lead1)
     lead1=a-b;
  if((b-a)>lead2)
     lead2=b-a;
i--;

}
if(lead1>lead2)
printf(“1 %d\n”,lead1);
else
printf(“2 %d\n”,lead2);
return(0);
}

I have written program in python and its showing NZEC.

The Lead Game:

i = 0

score_dif= 0
p1_score,p2_score = 0,0

while True:
rounds = int(raw_input())
if rounds <= 10000:break
else : continue
while i < rounds :

p1_score,p2_score = map(int, raw_input().split())
if (1 <= p1_score <= 1000) and (1 <= p2_score <= 1000) and (p1_score != p2_score):
	lead = p1_score - p2_score
	if p1_score > p2_score and lead > score_dif:
		score_dif = lead
    		
	elif p1_score < p2_score and abs(lead) > score_dif:
		score_dif = lead
   		i += 1	
		
	
else : continue

if score_dif >= 0:
print “1”, score_dif
else:
print “2”, abs(score_dif)

my code is working but it is saying false

include <stdio.h>

int main()
{
int a,d,i,min=-1,e;
scanf("%d",&a);
int b[a],c[a];
for(i=0;i<a;i++)
{
scanf("%d %d",&b[i],&c[i]);

}
for(i=0;i<a;i++)
{
if(c[i]>b[i])
{
d=c[i]-b[i];
if(d>min)
min=d;
if(d==min)
{
e=2;
}

}
if(c[i]<b[i])
{
    d=b[i]-c[i];
    if(d>min)
    min=d;
    if(d==min)
    {
        e=1;
    }
}

}
printf("%d %d",e,min);
return 0;

}

#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int n,j=1,f=0,t,k;
vector v;
scanf("%d",&n);
v.push_back(0);
for(int i=0;i<n;i++)
{
scanf("%d %d",&f,&k);
v.push_back(f);
v.push_back(k);
}
k=0;f=0;
for(int i=1;j<=n;i+=2,j++)
{
if(v[i]>v[i+1])
{
k=v[i]-v[i+1];
if(k>f)
{
t=1;
f=k;
}
}
else
{
k=v[i+1]-v[i];
if(k>f)
{
t=2;
f=k;
}
}
}
printf("%d %d\n",t,f);
return 0;
}

My code is giving the correct answer for all the testcases ive manually inputed and is still not getting accepted, please check it out and if there’s a mistake, cam somebody point it out ? !

@beroul, no it shouldn’t.

with the input:
3
20 100
20 100
1000 0

player 1 has the maximum lead of a 1000.
hence the output would be:
1 1000

2 Likes

I did it without use of arrays in a much small way.
The problem statement is pretty much vague. We need to sum up all the points up to the current round, find the lead at the end of the round, compute the lead and display the maximum lead out of the calculated leads and the the corresponding winner.

I did it in a small way. Check that out.
Correct solution to TLG

2 Likes

include

int main() {
int a,b,x,y,n,m,h,g;
scanf("%d",&a);
for(b=1;b<=a;b++)
{
scanf("%d %d",&x,&y);
if(x>y)
{
n = x-y;
m=1;
}
else
{
n = y-x;
m=2;
}
if(n>=h)
{
g=m;
h=n;
}
}
printf("%d %d",g,h);
return 0;
}

#include <stdio.h>

int main()
{
int a , i,j;
scanf("%d",&a);
int arr[a][3];

for (i=0;i<a;i++)
{
    for (j=0;j<2;j++)
        scanf("%d",&arr[i][j]);
}
for (i=0;i<a;i++)
{
    if (arr[i][0]> arr[i][1])
    {
        arr[i][2] = arr[i][0] - arr[i][1];
    }
    else if (arr[i][0]<arr[i][1])
    {
        arr[i][2] = arr[i][1] - arr[i][0];
    }
}
int *max = &arr[0][2];

for (i=0;i<a;i++)
{
    for ( j=i+1; j<a;j++)
    {
         if (*max < arr[j][2])
            *max = arr[j][2];
    }


}
if (*(max-2)> *(max-1))
printf("%d %d",1, *max);
if (*(max-2)< *(max-1))
printf("%d %d", 2 , *(max));
return 0;

}

Please Check my code.
Thanks

1 Like