< Back to forum

BRLADDER problem in code chef ..my code is showing the correct output but in codechef it is showing run time error

import java.io.*;
class today{
    public static void main(String args[])throws IOException{
int q;
        long a;
        long b;
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        q=Integer.parseInt(br.readLine());
        while(q>0)
        {
        a=Long.parseLong(br.readLine());
        b=Long.parseLong(br.readLine());
        if(((a-b)==2)||((b-a)==2))
            System.out.println("yes");
        else if(((a/2)-(b/2)==1))
                System.out.println("yes");
        else
            System.out.println("no");
            q--;
            }
    }
}

Asked by: Jayant_Mishra on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

Error is in the following line

a=Long.parseLong(br.readLine());
b=Long.parseLong(br.readLine());

In the input, two space seaparated integers are given in a single line.
br.readLine() reads entire line. Thus, for the first query of sample input(the one given in input example of the question) it will return string "1 4". By using Long.parseLong(), you are trying to convert a string(here it is "1 4") containing space into a number. This will result in runtime error.

The solution for this would be:

String[] temp = br.readLine().split();
a = Long.parseLong(temp[0]);
b = Long.parseLong(temp[1]);

 

Alternatively, you can use user-defined functions for faster input/output. Here, is the link for this. Below is the is the method to take input in the given case.

a = nl();
b = nl();

 

ROMIT_KUMAR last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.