TLG - Editorial

can somebody tell me on which test case my code gives the wrong output as i am getting WRONG ANSWER on submission.
#include
using namespace std;

int main() {
// your code goes here
int temp,n,i=0,j=0,w,l,flag=0;
int a[10001],b[10001];
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i]>>b[i];
}
l=a[0]-b[0];
if(l<0){w=2;l=-l;}
else w=1;
for(int j=1;j<n;j++)
{
a[j]=a[j]+a[j-1];
b[j]=b[j]+b[j-1];
temp=a[j]-b[j];
if(temp<0){temp=-temp;flag=1;}
else flag=0;
if(temp>l){flag=flag;l=temp;}
else flag=0;
}
if(flag==0)
cout<<"1 "<<l;
else
cout<<"2 "<<l;
return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,x,y,p1=0,p2=0,sum1=0,sum2=0;
cin>>n;
while(n–)
{
cin>>x>>y;
sum1 = sum1 + x;
sum2 = sum2 + y;
if(sum1>sum2)
{
if(sum1-sum2>p1)
p1 = sum1 - sum2;
}
else
{
if(sum2-sum1>p2)
p2 = sum2 - sum1;
}
}
if(p1>p2)
cout<<“1”<<" “<<p1<<endl;
else
cout<<“2”<<” "<<p2<<endl;
}

/* 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 scn = new Scanner(System.in);
int tc = scn.nextInt();
int max = Integer.MIN_VALUE;
int flag = 0;

	for(int t = 0; t<tc; t++){
	    int a = scn.nextInt();
	    int b = scn.nextInt();
	    
	    if(a - b > max){
	        flag = 1;
	        max = a-b;
	    }
	    if(b - a > max){
	        flag = 2;
	        max = b-a;
	    }
	    
	}
	System.out.print(flag+" "+max);
}

}

Can someone help me out in finding error within my code. it is working fine for given sample test case. however it is giving wrong answer when i submit it.

Thank You

hey bro was you able to figure out error in your code
i am also facing same issue .

Cant figure out the error in this code

#include <bits/stdc++.h>
using namespace std;

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
pair<int, int> value;
value.first = 1;
value.second = 0;
int lead;
int a, b;

while (t--)
{

    cin >> a >> b;
    lead = abs(a - b);
    if (lead > value.second)
    {
        value.second = lead;
        if (a > b)
            value.first = 1;
        else
            value.first = 2;
    }
}
cout << "\n"
     << value.first << " " << value.second;
return 0;

}

plz help me code is running fine but not being submitted due to time limit.It is taking 1.01 sec…(Time limit exceed).

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

T = int(input())
max=0
at = None
for i in range(T):
(a,b) = map(int,input().split())
if abs(a-b) > max:
max = abs(a-b)
if (a-b) > 0:
at = True
else:
at = False

if at == True:
print(“1”,max)
elif at == None:
x =0
else:
print(“2”,max)

why this is wrong
this is running for the given test cases.
and working for evey case i can think of.

What is wrong with my code provided test case works fine but gives wrong answer on submission ?

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.hasNextInt()?sc.nextInt():0;
		int c = -1;//Stores the maximum lead player1 will have
		int d = -1;//Stores the maximum lead player2 will have
		for(int i=0;i<n;i++){
		    int a = sc.hasNextInt()?sc.nextInt():0;//player1 score for nth round
		    int b = sc.hasNextInt()?sc.nextInt():0;//player2 score for nth round
		    int lead = Math.abs(a-b);
                    //c or d get assigned lead if current lead attained by them is higher than what they previously have
		    c = a>b && lead>c?lead:c; 
		    d = b>a && lead>d?lead:d;
		}
		System.out.println((c>d?1:2)+" "+(c>d?c:d));
	}
}

I am getting right answer as i run custom input but when i submit it is showing me runtime error can anyone tellme whats wrong .
#include
using namespace std;
int maxlead(int n, int a[])
{
int max=a[0];
for(int i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
return max;
}
int main()
{
int a[10],b[10],n,sum1=0,sum2=0,i,lead[10],leader,max1,max2;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i] >>b[i];
leader=a[i]-b[i];
if(leader>=0)
{
lead[i]=a[i]-b[i];
sum1=sum1+lead[i];
}
else
{
lead[i]=b[i]-a[i];
sum2=sum2+lead[i];
}
}
if(sum1>sum2)
{
max1=maxlead(n,lead);
cout<<"\n1 “<<max1;
}
else
{
max2=maxlead(n,lead);
cout<<”\n2 "<<max2;
}
return 0;
}

The question really has unclear explanation about what it means to say. Lets explain this question clearly to you with ans too…

Taking the example given the question :
the input given is:
5
140 82
89 134
90 110
112 106
88 90

  • In the round 1, the player 1 scores 140 points and player 2 scores 82 so the cumulative score of player 1 becomes 140 (as 0 + 140 = 140 ) and the cumulative score of player 2 becomes 82 (0+82).
    now as cumulative score of player 1 is greater then player 2 then we will store the lead score of player 1 ( which is 58 as 140-82 = 58) and 58>0(see note). so the lead score of player 1 is now 58.
    Note : Here the default lead scores of player 1 and 2 were 0.

  • now in round 2, player 1 score is 89 and player 2 score is 134. so the cumulative score of player 1 becomes 229 ( 140 + 89) and the cumulative score of player 2 becomes 216 (82+134). Now as cumulative score of player 1 is greater then the cumulative score of player2 so, first we will calculate the difference of cumulative scores of player 1 and player 2 and check whether the difference is greater than lead score of player 1 (as cumulative score of player 1 was greater then the cumulative score of player 2 in the second round ). as 13 (229-216) is less than 58, so the lead score of player 1 remains 58.

  • In the third round, the cumulative score of player 1 becomes 319 (229+90) and cumulative score of player 2 becomes 326 (216+110). now as cumulative score of player 2 is greater then player 1 so now we compare the lead score of player 2 with the difference of cumulative score of player 2 and player 1 (which is 326-319 =7 ). now as lead score of player 2 ( which is 0 ) is less than 7 so the lead score of player 2 is updated to 7.

  • now in the forth round, the cumulative score of player 1 becomes 431(319+112) and cumulative score of player 2 becomes 432 (326+106). now as cumulative score of player 2 is greater then player 1 so now we compare the lead score of player 2 with the difference of cumulative score of player 2 and player 1 (which is 432-431 =1 ). now as lead score of player 2 ( which is 7 ) is greater than 1 so the lead score of player 2 is not changed.

  • Now, in the fifth round, the cumulative score of player 1 becomes 519 (431+88) and cumulative score of player 2 becomes 522 (432+90). now as cumulative score of player 2 is greater then player 1 so now we compare the lead score of player 2 with the difference of cumulative score of player 2 and player 1 (which is 522-519 =3 ). now as lead score of player 2 ( which is 7 ) is greater than 3 so the lead score of player 2 is not changed .

now at last we compare the lead scores of both the players and as lead score of player 1 which is 58 is greater than lead score of player 2 which is 7 so player 1 is the winner.

code :
#include <stdio.h>

int main() {
	// your code goes here
	int i,n,a,b,max1=0,max2=0,cum2=0,cum1=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
	    scanf("%d%d",&a,&b);
	    cum1+=a;
	    cum2+=b;
	    if(cum1>cum2)
    {
    	if(max1<(cum1-cum2))
      		max1=(cum1-cum2);
    }
    else
    {
   		if(max2<(cum2-cum1))
      		max2=(cum2-cum1); 
    }
	}
	if(max1>max2)
	    printf("%d %d",1,max1);
	else
	    printf("%d %d",2,max2);
	return 0;
}
1 Like

i don’t know, how this code is not getting passed. They asked about maximum lead and I think this would be enough for that.

#include<bits/stdc++.h>

using namespace std;

int main(){

vector<pair<int,int>>v;

int n,si,ti,mx=0,q=0;cin>>n;while(n--){

    cin>>si>>ti;

    mx=max(abs(si-ti),mx);

    if(si>ti){

        v.emplace_back(make_pair((si-ti),1));

    }else if(si<ti){

        v.emplace_back(make_pair((ti-si),2));

    }

    for(int i=0;i<v.size();i++){

        if(v[i].first==mx){

            mx=v[i].first;

            q=v[i].second;

// break;

        }

    }

    cout<<q<<" "<<mx<<"\n"; 

}

return 0;

}

’ ’ ’ #include
using namespace std;
int main()
{
int rounds, ld, mxld = 0, p1, p2;
int p = 0;
cin >> rounds;
while (rounds–&&rounds<10000)
{
cin >> p1 >> p2;
if (p1 > p2)
{
ld = p1 - p2;
if (ld > mxld)
{
mxld = ld;
p = 1;
}
}
else
{
ld = p2 - p1;
if (ld > mxld)
{
mxld = ld;
p = 2;
}
}

}
cout << p << " " << mxld;
return 1;

’ ’ ’
Why does my code show runtime error? (I am aware of the fact that i have not taken into account the cumulative point scoring idea but i just would like to know what’s up with runtime error issue)

Can someone please tell what is wrong in this code?
#include
using namespace std;

int main() {
int n;
cin >> n;

int p1=0,p2=0,s1,s2,lead=0,x,sum1=0,sum2=0;


for(int i=0; i<n; i++)
{
    cin >> s1 >> s2;
    
    if(s1>=s2)
    {
            sum1+=s1;
            sum2+=s2;
            if(p1<s1-s2)
                p1=s1-s2;
            
    }
    
    else if(s2>=s1)
    {
            sum1+=s1;
            sum2+=s2;
            if(p2<s2-s1)
                p2=s2-s1;
    }
    
}

if(p1>p2)
    cout << "1 " << p1;
    
else if(p2>p1)
    cout << "2 " << p2;




return 0;

}

Here is my simple code for the problem TLG, using Python 3.
import math

n = int(input())
player = 0
max_score = 0
for _ in range(n):
    s, t = map(int, input().split())
    diff_score = int(math.fabs(s-t))
    if max_score < diff_score:
        max_score = diff_score
        if s < t:
            player = 2
        else:
            player = 1
print(player, max_score)

My submission status shows “Wrong Answer”!
Would anybody like to help me, what is wrong with the code??

#include
using namespace std;

int main() {
int T;
cin>>T;
int first[T];
int last[T];
for(int i=0; i<T; i++){
int N, M;
cin>>N>>M;
first[i]=N;
last[i]=M;
}
int high[T];
int highl[T];
int a=0;
int b=0;
for(int j=0; j<T; j++){
if(first[j]>last[j]){
high[a]=(first[j]-last[j]);
a+=1;
}

    else if(last[j] > first[j]){
        highl[b]=(last[j]-first[j]);
        b+=1;
    }
}
int high1=0;
int high2=0;
if(a==1){high1=high[0];}
else{
for(int k=0; k+1<a; k++){
    if(high[k]>high[k+1]){
        high1=high[k];
    }
    else{
        high1=high[k+1];
    }

}
}
if(b==1){high2=highl[0];}
else{
for(int g=0; g+1<b; g++){
    if(highl[g]>highl[g+1]){
        high2=highl[g];
    }
    else{
        high2=highl[g+1];
    }

}
}
if(high1>high2){
    cout<<"1 "<<high1<<endl;

}
else{
    cout<<"2 "<<high2<<endl;
}
return 0;

}

Can anyone tell me why my code is wrong…???

why is this not working

//please check why my code is not working

#include
using namespace std;

int main() {
int t;
cin>>t;
int p1;
int p2;
int max=0;
int lead=0;
while(t){
cin>>p1>>p2;
if(p1>p2 && abs(p1-p2)>max){
max=p1-p2;
lead=1;
}
if(p2>p1 && abs(p1-p2)>max){
max=p2-p1;
lead=2;

    }
    
      t=t-1;
    
}
cout<<lead<<" "<<max;

return 0;

}

#include <iostream>
using namespace std;

int main(){
  int N,winner,diff=0,maxLead = 0,s1 = 0,s2 = 0;
  cin>>N;
  for (int i=0;i<N;i++){
    int p1,p2;
    cin>>p1>>p2;
    s1 += p1;
    s2 += p2;
    diff = (s1 - s2) ;
    if (abs(diff) > maxLead){
      if (diff > 0){
        maxLead = diff;
        winner = 1;
      }
      else{
        maxLead = abs(diff);
        winner = 2;
      }  
    }
  }
  cout<<winner<<" "<<maxLead<<'\n';
  return 0;
}

Can anyone tell me…what 's wrong with my code…

#include<bits/stdc++.h>
using namespace std;
int main() {
long long x=0,y=0;
int t;
cin>>t;
while(t–){
long long a,b;
cin>>a>>b;
if(a>b)
x = max(a-b,x);
else
y = max(b-a,y);
}
if(x>y)
cout<<1<<" “<<x<<endl;
else
cout<<2<<” "<<y<<endl;
return 0;
}

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