Can anyone tell me whats wrong in my program. i have put sample input and the answer i correct. The problem is the lead game from practice problems. here is the link https://www.codechef.com/problems/TLG

/* 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 Codechef1
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner input = new Scanner(System.in);
int test, i, j, max, index=0;
test = input.nextInt();
int player1[] = new int[test];
int player2[] = new int[test];
int lead[] = new int[test];
int winner[] = new int[test];
for(i=0; i<test; i++) {
player1[i] = input.nextInt();
player2[i] = input.nextInt();
}
for(i=0; i<test; i++) {
if(player1[i]>player2[i]) {
winner[i] = 1;
}
else {
winner[i] = 2;
}
}
for(i=0; i<test; i++) {
lead[i] = Math.abs(player1[i]-player2[i]);
}
max = 0;
for(i=0; i<test; i++) {
if(lead[i]>max) {
max = lead[i];
index = i;
}
}
/for(i=0; i<test; i++) {
System.out.print(lead[i] + " ");
}
System.out.println();
for(i=0; i<test; i++) {
System.out.print(winner[i] + " ");
}
System.out.println();
System.out.println(max);
System.out.println(index);
/
System.out.println(winner[index] + " " + lead[index]);
}catch(Exception e) {
return;
}
}
}

Please send the link to your code

1 Like

That’s another link to the problem, not a link to your code :slight_smile:

1 Like

https://www.codechef.com/viewsolution/26104238

1 Like

Updated your code wrt the solution.

/* 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
		try {
		    Scanner input = new Scanner(System.in);
		    int test, i, max=0, index=0;
		    test = input.nextInt();
		    int player1[] = new int[test];
		    int player2[] = new int[test];
		    int lead = 0;
		    int winner = 0;
		    int player1Total = 0;
			int player2Total = 0;
		    for(i=0; i<test; i++) {
		        player1[i] = input.nextInt();
		        player2[i] = input.nextInt();
		    }
		    
		    for (i = 0; i < test; i++) {
				player1Total += player1[i];
				player2Total += player2[i];
				if (player1Total > player2Total) {
					lead = player1Total - player2Total;
					if (lead > max) {
						max = lead;
						winner = 1;
					}
				}
				else {
					lead = player2Total - player1Total;
					if (lead > max) {
						max = lead;
						winner = 2;
					}

				}
			}
    		    System.out.println(winner + " " + max);
		}catch(Exception e) {
		    return;
		}
	}
}

Okay, so you couldn’t understand the problem. So the lead after each round is the CUMULATIVE score till that round. You are only considering the score of THAT PARTICULAR ROUND.

Eg (n is 2)
(Format:
Round no, Player1 score, Player2 Score, Difference, Winner)

01 65 89 24 2
02 10 99 89 2

The answer should be 113
Your program is printing 89

Hints:

Hint 1

For both the players create a prefix sum array. If you don’t know, click here

Hint 2

For each round
if(max_score > abs(sum1[round] - sum2[round])
max_score = abs(sum1[round] - sum2[round]);
winner = winner[round];

Please note that it is 1 indexed

Hope you liked the small explanation of mine.:smiley::smiley:

1 Like