TLG - Editorial

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;

}

I think it can be done without arrays
Please anyone check my solution please let me know where i am going wrong

/*

Input:

5

140 82

89 134

90 110

112 106

88 90

Output:

1 58

*/

#include

using namespace std;

int main(){

int T,p1,p2,W,L,Lmax=0,Wmax=0;

cin>>T;

for(int i=0;i<T;i++){

    cin>>p1>>p2;

    if(p1>p2){

        W=1;

        L=abs(p1-p2);

    }else if(p2>p1){

        W=2;

        L=abs(p1-p2);

    }

    if(L>Lmax){

        Lmax=L;

        Wmax=W;

    }

}

cout<<Wmax<<" "<<Lmax<<endl;

}

I am getting wrong answer but what’s wrong here???

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static int findIndex(int arr[], int t)
{

    // if array is Null 
    if (arr == null) { 
        return -1; 
    } 

    // find length of array 
    int len = arr.length; 
    int i = 0; 

    // traverse in the array 
    while (i < len) { 

        // if the i-th element is t 
        // then return the index 
        if (arr[i] == t) { 
            return i; 
        } 
        else { 
            i = i + 1; 
        } 
    } 
    return -1; 
} 
public static void main (String[] args) throws java.lang.Exception
{
	Scanner scan =new Scanner(System.in);
	int t=scan.nextInt();
    int max=0,prev=0,p=0,i=0,j=0,index=0,n=t;
    int a[]=new int [t];
    int b[]=new int [t];

	    

	
	while(t-->0){
	    int x=scan.nextInt();
	    int y=scan.nextInt();
	  
	    if(x>y){
	        max=x-y;
	        a[j]=max;
	        b[j]=1;
	      //  if(j<t) j++;
	    }else if(y>x){
	        max=y-x;
	        a[j]=max;
	        b[j]=2;
	 	    // if(j<t) j++;
	    }else{
	        a[j]=0;
	        b[j]=0;
	      //  if(j<t) j++;
	    }
	    
	    if(max>prev){
	        i=max;
	       
	        prev=max;
	    }
	    if(prev>max){
	        i=prev;
	        
	        prev=prev;
	    }    
	  
	   if(j<n) j++;

	}
	index=findIndex(a,i);
	    System.out.println(b[index]+" "+i);
}

}

As I noticed the Input and the output given in the data of the question is certainly wrong if we consider this question to be a cumulative win and not a best lead win.
And can anybody help me to spot the error in my code
#include
using namespace std;

int main() {
int x,A,B,boss1,boss2,A1,B1,A2,B2;

cin>>x;
while(x--) {
    cin>>A>>B;

    if(A>B) {
        A1=A-B;
        if(A1>A2) {
            boss1=boss1+A1;
            A2=A1;
        }

    }

    else if(B>A) {
        B1=B-A;
        if(B1>B2) {
            boss2=boss2+B1;
            B2=B1;
        }

    }




}
 if(boss1>boss2)    {
        cout<<1<<" "<<boss1;
    }
    else {
        cout<<2<<" "<<boss2;
    }

}

idk why i implement the same logic but codechef always shows wrong answer even running the code i get the correct answer.

you are just comparing the scores at every round while in question scores are added at each round and then the winner and lead is calculated.