TLG - Editorial

whats wrong with my code :sweat_smile: when i run it against custom input it give write answer but when i submit it then wrong answer is display please if somebody know problem with my code please tell me . my code is this :point_down::point_down::point_down::point_down::point_down::point_down:
#include <stdio.h>

int main(void) {
// your code goes here

int n;
scanf("%d",&n);
int a[n],b[n],max=0,p=1,d;
for(int i=0; i<n; i++)
{
    scanf("%d%d",&a[i],&b[i]);
    d=a[i]-b[i];
    if(abs(d)>max)
    {
        max = abs(d);
        if(d<0) p=2;
        else p=1;
    }
}
printf("\n%d %d",p,max);
return 0;

}

What is wrong with my code??

#include
using namespace std;
int main()
{
long int player1;
long int player2,range;
int lead=0,p1=0,p2=0,diff,win=0;
cin>>range;
while(range–)
{
cin>>player1;
p1=p1+player1;
cin>>player2;
p2=p2+player2;
diff=p1-p2;
if(diff>lead)
{
lead=diff;
if(lead>0)
win=1;
else
win=2;
}
}
cout<<win<<" "<<lead<<endl;
return 0;
}

//can u tell me what we have to print?? it is not clear in the question whether we have to print the result and lead of the round 1 or every round result or end result??

//#include
using namespace std;

int main()
{
int n,lead;
cin>>n;
int i = 1,a[2],b[2];
a[0] = 0;
b[0] = 0;
while(n–)
{
cin>>a[1];
cin>>b[1];
a[0] = a[0] + a[1];
b[0] = b[0] + b[1];
}
if(a[0] > b[0])
cout<<"1 "<<a[0]-b[0]<<endl;
else
cout<<"2 "<<b[0]-a[0]<<endl;
// your code goes here
return 0;
}

Not necessary to use use array instead could be done with just the use of simple variables.
ie; Scores of player 1 and 2 are ‘a’ and ‘b’ respectively
Just keep count of the lead ( lead+=a-b; ) every time you input the scores.
If lead is positive it means player 1 is leading and if negative then it means player 2 is leading.
Also store the maximum lead in some variable.
code: CodeChef: Practical coding for everyone

hello guys!
my code is given below.what’s wrong with it .it’s correct in all test cases but when i submitted it’s give me wrong answer…

#include <bits/stdc++.h>
using namespace std;
void check(){
int suma=0,sumb=0,point=0,t,a,b;
cin>>t;
for(int i=0;i<t;i++){
cin>>a>>b;
suma+=a;
sumb+=b;
if(point<abs(suma-sumb))
point=suma-sumb;
}
if(point>0)
cout<<"1 "<<abs(point);
else
cout<<"2 "<<abs(point);
}

int main() {
check();
return 0;
}

Consider the testcase:

4
17 33
26 60
34 32
27 22

thanks man!!
i have found error…

1 Like

You Don’t even need a single Array

    Scanner sc = new Scanner(System.in);
                try {
                    int N = sc.nextInt();
                    int maxLeadValue = 0;
                    int winnerPlayer = 1;
                    int totalPlayerOne = 0;
                    int totalPlayerTwo = 0;
                    for (int i = 0; i < N; i++) {
                        int S = sc.nextInt();
                        int T = sc.nextInt();
                        totalPlayerOne = totalPlayerOne + S;
                        totalPlayerTwo = totalPlayerTwo + T;
                        int lead = totalPlayerOne > totalPlayerTwo ? totalPlayerOne - totalPlayerTwo : totalPlayerTwo - totalPlayerOne;
                        if (lead > maxLeadValue) {
                            maxLeadValue = lead;
                            winnerPlayer = totalPlayerOne > totalPlayerTwo ? 1 : 2;
                        }
                    }

                    System.out.println(winnerPlayer + " " + maxLeadValue);
                    
                } catch (NoSuchElementException nSEE) {
                    System.out.println("Wrong Input");
                } finally {
                    sc.close();
                }

no the ans should be 1 840 noob

thank you brother

1 Like

why is my answer wrong?
#include<bits/stdc++.h>
using namespace std;

int main(void)
{
int n;
cin>>n;
int maxx[n],w[n];
int i=0;
while(n–)
{
int s,t;
cin>>s>>t;
if(s-t>0)
{
maxx[i]=s-t;
w[i]=1;
i++;
continue;
}
else
{
maxx[i]=t-s;
w[i]=2;
i++;
continue;
}
}
int j=distance(maxx,max_element(maxx,maxx+i));
cout<<w[j]<<" "<<*max_element(maxx,maxx+i);
return 0;
}

Okay, I have seen lot of implementations here. Some creative, some really not required complications.
Here is my clean code

n = int(input())
p1_score = 0
max_lead = [-1,-1]
p2_score = 0
while(n>0):
    p1, p2 = map(int, input().split())
    p1_score += p1
    p2_score += p2
    # print(p1_score, p2_score)
    if(p1_score > p2_score and (abs(p1_score - p2_score) > max_lead[1])):
        max_lead[1] = (p1_score - p2_score)
        max_lead[0] = 1
    elif(p2_score > p1_score and abs(p2_score - p1_score) > max_lead[1]):
        max_lead[1] = (p2_score - p1_score)
        max_lead[0] = 2
        
    # print(max_lead[0], max_lead[1])
    n -= 1
    
print(max_lead[0], max_lead[1])

can someone review my code ?
Again there are tons of ways to do it but why not this?
works perfectly everywhere, even in custom inputs, fails during submission :

tc = int(input())
list_1 = []
list_2 = []

for i in range(tc):
(a,b) = map(int,input().split(’ '))
list_1.append(a)
list_2.append(b)

max_diff = 0
for j in range(tc):
if list_1[j]>list_2[j] and max_diff < list_1[j]-list_2[j] :
winner = 1
max_diff = list_1[j]-list_2[j]
elif list_1[j]<list_2[j] and max_diff < list_2[j]-list_1[j]:
winner = 2
max_diff = list_2[j]-list_1[j]

print(’{0} {1}’.format(winner,max_diff))

Value of m should be constant as1

Array initialition at beginning

Bez of pair

Initialition of i

d(i) should be there

First condition of I till a-1

#include
using namespace std;

int main()
{
int r,max=0,winner,lead;
cin>>r;
int a[r],b[r];
for(int i=0;i<r;i++)
{
cin>>a[i];
cin>>b[i];
}
for(int i=0;i<r;i++)
{
if(a[i]>b[i])
{
lead=a[i]-b[i];
if(lead>=max)
{
max=lead;
winner=1;
}
}
else if(b[i]>a[i])
{
lead=b[i]-a[i];
if(lead>=max)
{
max=lead;
winner=2;
}
}
}
cout<<winner<<" "<<max;
return 0;
}

i used this code to solve the “The Lead Game” TLG problem. I am getting right output for given test case but upon submitting it shows wrong answer. what could have been gone wrong? please help.