Need help in java.

I have noticed that making instances for User-defined classes in JAVA makes code slower. Sometimes I got TLE.

I have solved a question on HackerRank. I have used User-defined class(Pair) first which gives TLE then replacing with Nby2 matrix it got Accepted.

Since, Making these nested classes makes implementation easier. Is there any way by which User-defined classes will become faster?

Thanks in Advance.

The links to the solutions are not working for me, so whatever I am saying is from my own experience and not after checking your code.

As far as user defined classes are concerned, I have used them a lot (on codechef at least) , and never had any performance issues. So the issue might be with the hackerrank judge.

I am not sure but declaring the user defined class as a static inner class to your main class might be a bit faster.