Run error in one of the testcase

@mudit2jain
There is null pointer exception at line 74 while taking input.
It occurs when you are using property of a null object.
like in your code array.split(" ");
Please correct your input method if it still fails,please revert back here.

Property of a null object?
I am sorry, i dont understand the slang.

How to resolve it?
Should i use scanner?
If yes, then isn’t Scanner slow than BufferedReader, how to optimize it then?
if no, then how to resolve it in BufferedReader itself?

I have these bunch of questions. I would be obliged if you can clear them.

@mudit2jain
Yes,Scanner is slow but once try to solve the problem using Scanner,then I will provide the alternative.

I tried with scanner and it passed the testcases.

Can you please provide the alternative if Scanner is slow?

Yes,the work done using Scanner can be done using BufferReader too.

Thats exactly what i am asking. How?
As i used bufferedReader which was giving error. So what statements will i need to write so that there will be no exception in taking input.

@mudit2jain
In the test case,if we observe
3 4
1 3 5 7
2 4 6 8
0 9 10 11
We are having line break after each array input,hence this line:
String info = br.readLine();
only takes 3 4 into String info,due to line break.
Hence, Everything works well for Test Case 1.

On the Other hand,Second test case has concatenated input as 4 1000 then 1000 arrays without any line break.
For Illustration=>
Consider above Input as 3 4 1 3 5 7 2 4 6 8 0 9 10 11,the input format is correct and will work for Scanner.But Coming to taking input using BufferReader,
String info = br.readLine();
We now have String info as 3 4 1 3 5 7 2 4 6 8 0 9 10 11,which should be 3 4 as per consideration.
Hence,when the input is concatenated just separated by a space,we are getting everything in String info,which isn’t the case when using Scanner as we are taking input using
int k = sc.nextInt();
int n = sc.nextInt();
It will work in both case,ie. in input with break line and input without break line or concatenated input.
For more understanding,Try to run your BufferReader code with this input 3 4 1 3 5 7 2 4 6 8 0 9 10 11.
It will not work and you will get better understanding.

I appreciate the way you explained me whole concept and i understood it properly, but as per our previous conversation Scanner is slow and bufferedReader is causing the above specified error. So, you are saying there is no way such errors can be stopped using bufferedreader only? As we want the best combination of fast and accurate input?

@mudit2jain
Yes,BufferReader has advantage with time,but it has its own limitation too.
the function br.readline() reads till when it doesn’t find any line break.
In this problem,the alternative(BufferReader) won’t work as There are two types of Inputs given.
1st test case has different type,2nd test case has different type(without line break).
In first case, only 3 4 are there in String info.(because no line break).
In second case,everything gets accumulated in String info(because no line break and it continued to read till end).


Please read the highlighted line.

1 Like