TLG - Editorial

#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!?

Can anyone tell me what’s wrong in this?
import java.util.Scanner;

public class Tlg {

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	int a[][]=new int[n][2];
	for(int i=0;i<n;i++) {
		int p1=sc.nextInt();
		int p2=sc.nextInt();
		if(p1>p2) {
			a[i][0]=p1-p2;
			a[i][1]=1;
		}else {
			a[i][0]=p2-p1;
			a[i][1]=2;
		}
	}
	int max=0;
	int win=0;
	for(int i=0;i<n;i++) {
		while(max<a[i][0]) {
			max=a[i][0];
			win=a[i][1];
		}
	}
	System.out.println(win+" "+max);

}

}

can you explain your solution please?


c++

Click Here to register for upcoming contest

PCO12020 going live in # 3 minutes
Registration ends in 1 day

1 Like

rounds = int(input())

maxLead =0
winner = 0
p1C = 0
p2C = 0

for i in range(rounds):
p1s,p2s = map(int,input().split())

p1C += p1s
p2C += p2s

if p1C > p2C and maxLead < p1C- p2C:
    winner = 1
    maxLead = p1C- p2C
    
if p2C > p1C and maxLead < p2C- p1C# p1C > p2C and maxLead < p2C- p1C: Error
    winner = 2
    maxLead = p2C- p1C

print(winner,maxLead)

I got 1 840
and compiler saying wrong answer

what is wrong with my solution because on every input it is giving right answer but during submission of solution it is showing wrong answer

#include<bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

int max_lead = 0;
vector<int> a(n);
vector<int> b(n);

for (int i = 0; i < n; i++) {
	cin >> a[i] >> b[i];
}

int sum_a = 0, sum_b = 0;
bool abool = false, bbool = false;

for (int i = 0; i < n; i++) {
	sum_a += a[i];
	sum_b += b[i];

	int lead = 0;

	if (sum_a > sum_b) {
		lead = (sum_a - sum_b);
		if (lead > max_lead) {
			abool = true;
			max_lead = lead;
		}
	} else if (sum_b > sum_a) {
		lead = (sum_b - sum_a);
		if (lead > max_lead) {
			bbool = true;
			max_lead = lead;
		}
	}
}

if (abool)
	cout << "1" << " " << max_lead << "\n";
else
	cout << "2" << " " << max_lead << "\n";

return 0;

}
getting WA while submitting but getting all test cases passed

I am getting wrong answer don’t know why please someone help…

#include<bits/stdc++.h>
using namespace std;
#define ll long long
main() {
int t;
ll a, b;
cin >> t;
int p1[t], p2[t];
for (int i = 0; i < t; ++i) {
cin >> a >> b;
if (a > b)
{
p1[i] = a - b;
p2[i] = 0;
} else {
p2[i] = b - a;
p1[i] = 0;
}
}
sort(p1, p1 + t); sort(p2, p2 + t);
if (p1[t - 1] > p2[t - 1])
{
cout << "1 " << p1[t - 1];
} else {
cout << "2 " << p2[t - 1];
}

}

I am not able to understand why is it giving wrong answer here but working perfectly on pycharm.
n = int(input())
a = []
b = []
c = []
d = []
for _ in range(n):
e,f = map(int,input().split(’ '))
a.append(e)
b.append(f)
for i in range(n):
if a[i] > b[i]:
c.append(a[i] - b[i])
else:
d.append(b[i] - a[i])
if max© > max(d):
print(1, max© , sep= " ")
else:
print(2, max(d) , sep= " ")

Can someone help me with the solution
It was working on codeblocks

#include <stdio.h>

int main(void) {
// your code goes here
int n,lead,P1,maxLead=0,P2,W;
scanf("%d",&n);
if(n<=10000){
while(n–){
scanf("%d",&P1);
scanf("%d",&P2);
if(P1>=P2){
lead=P1-P2;
}
else{
lead=P2-P1;
}

    if(maxLead<lead)
	 {
	     maxLead=lead;
	     
	      if(P1>P2){
	    W=1;
	    }
	    else{
	    W=2;
	    }
     }
    
  }
  printf("%d %d\n",W,maxLead);
}
return 0;

}

Try this one may it;s helpfull for u

#include
using namespace std;

int main() {
int n;
cin>>n;
int i=0;
int S;
int T;
int s1=0,s2=0;
long long int sum1=0,sum2=0;
while(i<n) {
cin>>S>>T;
sum1+=S;
sum2+=T;
if(sum1>sum2) {
s1=sum1-sum2>=s1?sum1-sum2:s1;
}
else {
s2=sum2-sum1>=s2?sum2-sum1:s2;
}
i++;
}
if(s1>s2) {
cout<<“1”<<" “<<s1;
}
else {
cout<<“2”<<” "<<s2;
}
return 0;
}

Do without taking array just take simple variable;

#include
using namespace std ;

int main(){
int total_rounds ;
cin >> total_rounds ;
int player_one[total_rounds] ;
int player_two[total_rounds] ;
int lead[total_rounds] ;
for (int i = 0; i < total_rounds; ++i) {
cin >> player_one[i] >> player_two[i] ;
if (player_one[i] > player_two[i]){
lead[i] = player_one[i] - player_two[i] ;
} else{
lead[i] = player_two[i] - player_one[i] ;
}

}
int largest{INT16_MIN};
for (int i = 0; i < total_rounds; ++i) {
    if (lead[i] > largest){
        largest = lead[i];
    }
}
for (int i = 0; i < total_rounds; ++i) {
    if (player_one[i] - player_two[i] == largest){
        cout << 1 << " ";
    }
    if (player_two[i] - player_one[i] == largest){
        cout << 2 << " ";
    }
}
cout << largest << '\n' ;






return 0 ;

}

//NOT GETTING SUBMITTED SOMEONE HELP

Why to use four arrays, here is my short solution in C++
#include
#include<bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
int i,p,q;
int l=0,k=0;
int lead=INT_MIN;
int leader;

for(i=0;i<n;i++)
{
    cin>>p>>q;
    k+=p;
    l+=q;
    if(abs(l-k)>lead)
    {
        lead=abs(l-k);
        leader=k>l?1:2;
    }
}
    
cout<<leader<<" "<<lead<<"\n";

return 0;

}