Tht means the question has a better and efficient implementation. Let me give u a hint - Think bitwise!
I have assumed here that the inbuilt java function is not doing the same
First u may try replacing Scanner with BufferedReader because scanner is very slow.Also use BufferedWriter to print. May be faster I/O will help. If u get TLE even after these changes , then u have to make ur own algorithm.