Run error for the 2nd test case. rest cases have passed

import java.util.;
import java.io.
;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s1[] = br.readLine().trim().split("\s+");
String s2[] = br.readLine().trim().split("\s+");

    HashMap<String, Integer> map = new HashMap<>();
    ArrayList<String> list = new ArrayList<>();

    for(int i = 0; i < n; i++) {
        if(map.containsKey(s1[i])) {
            map.put(s1[i], map.get(s1[i]) + 1);
        } else {
            map.put(s1[i], 1);
        }
    }

    for(int i = 0; i < n; i++) {
        if(map.containsKey(s2[i])) {
            if(map.get(s2[i]) > 0) {
                list.add(s2[i]);
                map.put(s2[i], map.get(s2[i]) - 1);
            }
        }
    }

    Collections.sort(list);
    System.out.print(list);
}

}

Your logic is correct, there is some problem with the input format. Actually when you are taking input using buffered reader, spaces matter a lot. So, in the second test case there is a space after the value of n is given. That is causing your input mismatch problems.

For example: 7
1 2 3 1 2 4 1
2 1 3 1 5 2 2

In this sample test case, if I put a space after 7, it will cause a problem because it is taking input as a string, so it is not able to convert "7 " to int. You can either try to find a way around this by using split function, or simply use scanner class.

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.