TLG - Editorial

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

I also used the same approach but why my code is not acception. please check my code and explaination where I made mistake.
link:
Solution: 56568706 | CodeChef

Pls see what is wrong in my solution
https://www.codechef.com/viewsolution/56815158

you are finding the lead for individual rounds…you have to add scores of each round and then find the overall lead, so basically your lead array is incorrect.

Here is my cpp code if it helps:
#include <bits/stdc++.h>
using namespace std;

int main() {
int n,i,max,min,sum1=0,sum2=0;
cin >> n;
int p1[n],p2[n],lead[n];
for (i=0;i<n;i++){
cin >> p1[i];
cin >> p2[i];
}
for(i=0;i<n;i++){
sum1= sum1+p1[i];
sum2= sum2+p2[i];
lead[i]=sum1-sum2;
}
max = lead[0];
for (i = 0; i < n; i++)
{
if (max < lead[i])
max = lead[i];
}
min = lead[0];
for (i = 0; i < n; i++)
{
if (min > lead[i])
min = lead[i];
}
min = -min;
if (max>min){
cout << "1 " << max;
}
if (max<min){
cout << "2 " << min;
}
return 0;
}

What’s wrong with my solution…

#include
using namespace std;

int main()
{
int i=0,l=0,w=0;
cin >> i;
while (i–)
{
int a,b,k=0;
cin>>a>>b;
if(a>b)
{
k = a-b;
}
else
{
k = b-a;
}
if(l <= k)
{
l = k;
if(a>b) w=1;
else w = 2;
}
}
cout<<w<<" "<<l;
return 0;
}

Here is the python code for TLG.

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

Please Check my code:
T = int(input())
Z = list()
for i in range(T):
X,Y = [int(i) for i in input().split()]
Z.append(X-Y)
if max(Z) > abs(min(Z)):
print(1,max(Z))
else:
print(2,abs(min(Z)))
Give me a suggestion What is wrong with this code>

cumu_score = []
lead = 0
for _ in range(int(input())):
p1,p2 =map(int,input().split())
diff = abs(p1 - p2)
if p1 > p2 :
cumu_score.append((1,diff))
else:
cumu_score.append((2,diff))

cumu_score.sort(key = lambda x: x[1])
w = cumu_score[len(cumu_score) - 1][0]
l = cumu_score[len(cumu_score) - 1][1]
print(w,l)

It work correctly. and pass many test cases. but still shows WA test case failed… don’t understand what’s going wrong.?