SPOJ : BABY -java tle

Hey,
http://www.spoj.com/problems/BABY/ /n
Tried a O(2^n * n^2) soln. Got TLE.
Then found a cpp soln online of O(2^n * n).
Implemented in java and got TLE .
Implemented in C and the same soln passed .
Tested both on ideone with n=16 . \n
java runtime=0.17s
c runtime = 0.03s \n
Why is there so much diff in runtimes of java and c ?
Is there a better approach to this problem (java passable) ??

See you have to optimize your code a lot and have to use faster IO templates in JAVA for strict timelimit problems because JAVA is a slower language as compared to C/C++.

It takes much time in IO process.

Even i have faced such problems. Here it is.

JAVA solution : - https://www.codechef.com/viewsolution/8884493 (got TLE)

C solution : - https://www.codechef.com/viewsolution/8884719 (got accepted)

Better idea would be - you may go for C,C++ for such problems.

Happy coding. :slight_smile:

4 Likes

nice explanation :slight_smile:

1 Like

hmm :expressionless:
will have to switch to c for such tight time constraint problems

//