TLG - Editorial

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

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