This code shows RunTime error in last test case when i take the input using BufferedReader. But when i change input method to Scanner , it passes 100%. What is the reason for that?

import java.util.*;

import java.io.*;

public class Main {

public static void main(String args[])throws IOException {

    // Your Code Here

	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

	int n=Integer.parseInt(br.readLine());

	String arr[]=br.readLine().split(" ");

	String brr[]=br.readLine().split(" ");

	HashMap<Long,Long> s=new HashMap<>();

	for(int i=0;i<n;i++){

		long x=Long.parseLong(arr[i]);

		if(s.containsKey(x)){

			s.replace(x,s.get(x)+1);

		}

		else s.put(x,1L);

	}

	// SortedSet<Integer> r=new TreeSet<>();

	List<Long> res=new ArrayList<>();

	for(int i=0;i<n;i++){

		long x=Long.parseLong(brr[i]);

		if(s.containsKey(x) && s.get(x)>0){

			res.add(x);

			s.replace(x,s.get(x)-1);

		}

	}

	Collections.sort(res);

	System.out.println(res);

}

}

There’s a problem with the formatting of one test case, what you can do is first trim the string and then convert to Integer. br.readLine().trim() , this way it should pass all cases.

Thanks, that helped. :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.