Optimized Java Solution for spoj ACMCEG2B problem

My solution: https://ide.codingblocks.com/s/75755

With the code I have written, I’m facing Time Limit Exceeded problem.

Can someone please help me by telling an optimized approach in Java?

try using Fast I/O. Normally we get TLE problem in java if we don’t use Fast I/O because java is slower as compared to c++.

I have also tried using BufferedReader but that also is giving Time Limit Exceeded.

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.