TLG - Editorial

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.

Why is my code not being accepted?

My solution
#include
using namespace std;

int main() {
int n; cin >> n;
int player1Score; cin >> player1Score;
int player2Score; cin >> player2Score;
int maxLead = player1Score - player2Score;
for(int i = 1; i < n; i++)
{
cin >> player1Score >> player2Score;
if(abs(maxLead) < abs(player1Score - player2Score))
maxLead = player1Score - player2Score;
}
if(maxLead > 0)
cout << "1 " << maxLead;
else
cout << "2 " << abs(maxLead);
return 0;
}

#include <stdio.h>
int main(void) {
	// your code goes here
	int t,num_a,num_b,diff,i=1,winner,player,grtnum;
	scanf("%d",&t);
	int arr[t];
	 scanf("%d%d",&num_a,&num_b);
	    if(num_a>num_b){
	        diff=num_a-num_b;
	        player=1;
	    }
	    else{
	        diff=num_b-num_a;
	        player=2; 
	    }
        winner=player;
	    grtnum=diff;
	while(t>i){{
	    scanf("%d%d",&num_a,&num_b);
	    if(num_a>num_b){
	        diff=num_a-num_b;
	        player=1;
	    }
	    else{
	        diff=num_b-num_a;
	        player=2; 
	    }
	}
	   
	    if(diff>grtnum){
	        grtnum=diff;
	        winner=player;
	    }
	    i++;
	}
    printf("%d %d",winner,grtnum);
	return 0;
}

Please help me out i have tried several custom inputs and it is showing answer correctly but when i submit it ,it shows wrong answer. Please review the code and help me out.

my code is absolutely fine for ALL custom inputs but when i submit it is showing wrong answer can someone please help?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int r;
cin>>r;
int p1[r],p2[r],l[r],n,p[r];
for(int j=0;j<r;j++)
{
cin>>p1[j]>>p2[j];

        l[j]=abs(p1[j]-p2[j]);



}

int* v;
v=max_element(l,l+r);
int x;
x=distance(l,max_element(l,l+r));



if(p1[x]>p2[x])
    cout<<"1"<<" "<<l[x];
 else
       cout<<"2"<<" "<<l[x];


return 0;

}

1 Like

yes the output is
2 34
which is correct!?