TLG - Editorial

I think we don’t need arrays for this problem. Java solution :-

 Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    
    int cummulativeScorePlayer1 = 0;
    int cummulativeScorePlayer2 = 0;
    int lead = 0;
    int max = Integer.MIN_VALUE;
    int playerWon = 0;

    while(T-- > 0){
      
      int score1 = sc.nextInt();
      int score2 = sc.nextInt();
      
      cummulativeScorePlayer1 = cummulativeScorePlayer1 + score1;
      cummulativeScorePlayer2 = cummulativeScorePlayer2 + score2;
      
      if(cummulativeScorePlayer1 > cummulativeScorePlayer2){
          
          lead = cummulativeScorePlayer1 - cummulativeScorePlayer2;
          if(lead > max){
              max = lead;
              playerWon = 1;
          }
      }
      else if(cummulativeScorePlayer2 > cummulativeScorePlayer1){
          
          lead = cummulativeScorePlayer2 - cummulativeScorePlayer1;
          if(lead > max){
              max = lead;
              playerWon = 2;
          }
      }
      
    }
    System.out.println(playerWon+" "+max);

can somebody explain whats wrong with my code ?

n=int(input())
a1=[]
b1=[]
g=1
h=2
x=0
y=0
for i in range(n):
a,b= map(int,input().split())
#x,y = map(int, input().split())
a=x+a
b=y+b
if (a>b):
a1.append((a-b))

else:
    b1.append((b-a))
x=a
y=b

a1.sort()
b1.sort()
if (a1[-1]>b1[-1]):
print(g,a1[-1])
else:
print([h,b1[-1]])

All code is right and manually giving write answer but during compiling getting error

Plz anyone have a look on my code and help if you can

Problem Link:- Solution: 49831330 | CodeChef

I have used Array method in c# and most of the test cases are right. CodeChef is saying wrong answer.
I wanna know whats the mistake… let me know…
[CodeChef: Practical coding for everyone](https://My code)

#include
using namespace std;

int main() {

int round,maxi=0,i,n;
cin>>round;
int p1[round],p2[round],result[round],player[round];

for(i=0;i<round;i++)
{  
   cin>>p1[i]>>p2[i];
   if(p1[i]<p2[i]){
       result[i]=p2[i]-p1[i];player[i]=2;}
   else{
       result[i]=p1[i]-p2[i];player[i]=1;}
   if(result[i]>maxi){
    maxi=result[i];n=player[i];}
       
}


    cout<<n<<" "<<maxi<<endl;
return 0;

}
can anybody help me with this?..why this code is not acceptable

Hi guys ,
can anyone tell me what’s wrong with my code, I gave multiple inputs and all shows correct answer but when I submit is says wrong.
her is my code
https://www.codechef.com/viewsolution/53337328

thanks in advance

Thank you ssjgz

1 Like

whats wrong with this solution??

#include<bits/stdc++.h>

using namespace std;

int main(){

// freopen("input.txt", "r", stdin);

int n;

cin>>n;

int a, b ;

int amax=-1 , bmax=-1;

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

    cin>>a>>b;

    if( a > b){

        amax = max(amax, a-b);

    }

    else{

        bmax = max(bmax, b-a);

    }

}

if( amax > bmax){

    cout<<1<<" "<<amax<<endl;

}

else{

    cout<<2<<" "<<bmax<<endl;

}

return 0;

}

My code is working if I run but not when I submit…
what wrong with this code?

#include<iostream>
int main() {
    int t, w=0;
    std::cin>>t;
    int p1[t], p2[t], total[t], P1CLead, P2CLead;
    for (int i=0; i<t;i++){
        std::cin>>p1[i]>>p2[i]; 
    }
    for (int i=0; i<t; i++){
        
        if (p1[i]>p2[i]) {
            total[i] = p1[i]-p2[i];
            if(i==1) P1CLead=total[i];
            if(total[i]>P1CLead) P1CLead=total[i];
        } 
        if (p2[i]>p1[i]) {
            total[i] = p2[i]-p1[i];

            if(i==1) P2CLead=total[i];
            if(total[i]>P2CLead) P2CLead=total[i];
        }
    } 
    if(P1CLead>P2CLead) {
        std::cout<<1<<" "<<P1CLead;
    }
    if(P2CLead>P2CLead) {
        std::cout<<2<<" "<<P2CLead;
    }
}

Pay attention to compiler warnings!

[simon@simon-laptop][13:40:31]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling ljn7-TLG.cpp
Executing command:
  g++ -std=c++17 ljn7-TLG.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
ljn7-TLG.cpp: In function ‘int main()’:
ljn7-TLG.cpp:26:15: warning: self-comparison always evaluates to false [-Wtautological-compare]
     if(P2CLead>P2CLead) {
        ~~~~~~~^~~~~~~~
ljn7-TLG.cpp:3:12: warning: unused variable ‘w’ [-Wunused-variable]
     int t, w=0;
            ^
ljn7-TLG.cpp:23:5: warning: ‘P2CLead’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     if(P1CLead>P2CLead) {
     ^~
ljn7-TLG.cpp:24:26: warning: ‘P1CLead’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         std::cout<<1<<" "<<P1CLead;
         ~~~~~~~~~~~~~~~~~^~~~~~~~~
Successful

Can anyone show me why this code doesnt work?

#include <bits/stdc++.h>
using namespace std;
int main(){
int i, pone, ptwo, winner, lead;
cin >> i;
int gameRound[i];
for(int a=0; a<i; a++){
cin >> pone >> ptwo;
if(pone>ptwo) gameRound[a] = ((pone - ptwo) * 10) + 1;
else gameRound[a] = ((ptwo - pone) * 10) + 2;
}
sort(gameRound, gameRound + i);
winner = gameRound[i-1] - ((gameRound[i-1]/10)*10);
lead = gameRound[i-1]/10;
cout << winner << " " << lead;
return 0;
}

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

I dont know why my solution is not being accepted

See my post directly above yours :slight_smile:

@beroul
can you help me
#include
using namespace std;

int main()
{
int t,c=0,d=0,e=0,f=0;
cin >>t;
for(int i=0;i<t;i++)
{
int a,b;
cin >>a>>b;
e = e+a;
f=f+b;
if(a>b)
{
if( (a-b) >c)
{c = (a-b);}
}
else
{
if( (b-a) >d)
{d = (b-a);}
}

}
if(c>d)
{
cout<< “1”<< " "<< (e-f);
}
else
cout<< “2”<< " "<< (f-e);

return 0; 

}

i used the same approach but why my code is not accepting. please check it ones and say where i made mistake
link:
Solution: 56568706 | CodeChef