Help me in solving TLG problem

My issue

I don’t whats the problem in this code. Why its showing wrong answer while it runs success fully but not passing some cases.

My code

/* 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 void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner read=new Scanner(System.in);
		int n=read.nextInt();
		int i=0;
		int []T=new int[n];
		int [] S=new int[n];
		int diff=0;
		int max=0;
		while((n--)!=0)
		{
		        T[i]=read.nextInt();
		        S[i]=read.nextInt();
		        if(T[i]>S[i])
		        diff=T[i]-S[i];
		        else
		        diff=S[i]-T[i];
		         if(Math.abs(diff)>Math.abs(max))
	        max=diff; // maxmimum lead changed.
		}
	    if(max>0) //if current lead if greater than maximum lead .means the mamximum lead would be change . winner would also be updated.
	        
	    System.out.print(1+" "+(max));
	    else
	    System.out.print(2+" "+Math.abs(max));
		// your code goes here
		
	}
}

Problem Link: TLG Problem - CodeChef

@vishu_2650

in my code i am checking the greatest difference in the game and keep updating it in a variable if i find any bigger difference and then i check if the value of difference is positive or negative which decides whether the winner is player 1 or 2

here is my code below
hope this helps!!

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

int main() {
// your code goes here
int lead=0,maxdiff=0,display=0;
int n;
cin>>n;
int a[n],b[n];

for(int i=0;i<n;i++)
{
    cin>>a[i]>>b[i];
}
	for(int i=0;i<n;i++)
{
    lead+=a[i]-b[i];
    if(abs(lead)>maxdiff)
    {
        maxdiff=abs(lead);
        display=(lead>0)?1:2;
    }
}
	cout<<display<<" "<<maxdiff<<endl;

return 0;

}

1 Like