Have a look in the following question , no matter how much efficiently you write the solution for this , using Scanner class is resulting in the time out .
Question :
You have given an array , having elements . Now for given query you have provided an integer and you have to find the Min Sum that is the sum of all elements in the array whose value less than the given integer provided into query.
Input format:
First line: An integer denoting number of array elements. Second line: space seperated integer denoting array elements. Third line: An integer denoting number of query. For each query: A new line contain an integer
Output format: For each query print a single integer that is Min Sum in a new line.
Just by using Buffered Reader
instead of Scanner class
and Buffered Writer
instead of System.out.print
, makes this code twice as faster and all the test cases passes easily , what is the reason behind this ?
Advertisement
Answer
BufferedReader is significantly faster than Scanner as it has a larger buffer memory than Scanner. You can see the difference between the two in this discussion.