Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

What's wrong with my code?

$
0
0

The problem name is The Lead Game[easy section TLG].I've tried all possible test cases, read all comments, even seen some solutions(I shouldn't have done that) and now come to the last resort.HELP ME[No. It's not the usual cumulative error]

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.BufferedReader;

public class TheLeadGame {
    public static void main(String[] args){
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter pw = new PrintWriter(System.out);
            int t = Integer.parseInt(br.readLine());
            boolean play1 = false, play2 = false;
            long diff = 10000, max = -1, sum1 = 0, sum2 = 0;
            while(t>0){
                String str[] = br.readLine().split(" ");
                int player1 = Integer.parseInt(str[0]);
                int player2 = Integer.parseInt(str[1]); 
                sum1 += player1;
                sum2 += player2;
                if(sum1>sum2){
                    diff=sum1-sum2;
                    if(diff>max){
                        max = diff;
                        play1 = true;
                    }
                }
                else{
                    diff=sum2-sum1;
                    if(diff>max){
                        max = diff;
                        play2 = true;
                    }
                }
                t--;
            }   
            if(play1 == true){
                pw.println("1 "+max);
            }
            else{
                pw.println("2 "+max);
            }
            pw.flush();
        }
        catch(IOException e){
            e.getMessage();
        }
    }
}

Help me and if possible give some test cases as applicable


Viewing all articles
Browse latest Browse all 40121

Trending Articles