SPOJ ARITH2 using ScriptEngineManager gives TLE. Please help

This post was flagged by the community and is temporarily hidden.

I know the problem can be done using switch case very easily

Do it! I think that first thing you should try when you want to use super-weird solution is to have normal one.


your solution returns incorrect result for input

1000000001 * 1000000002 =

and throws exception for


There are some micro optimizations you can do, but why to do those, when probably engine.eval() call is the “bottle neck”?

What would you answer to someone if he/she asks: “Hello guys, my program is getting TLE. I know it can be easily done using scanf and printf, but I learnt about cin/cout recently and want to apply it for this particular program.”?

I do not want to be rude, but this is very typical for newbies: “Guys, what is the fastest I/O reading operation, because my O(N^2) solution is getting TLE for N=1000000…”, see?

If you have working (AC) version, than you can do some experiments how much slower this revolutionary approach of using ScriptEngineManager is, for some big N, let say 100000 and input file is

1 * 2 =
2 * 3 =
100000 * 100001 =

This post was flagged by the community and is temporarily hidden.

About Spaces in the Input File

actually there are spaces in the ip file after every number and operator(except ‘=’, it has a ‘\n’)…cause my input fxn takes an extra char to terminate i/p…and it was AC…so there is a space b/w all elements…this is my code…LINK!!!

See those three codes:

It is you solution slightly modified:

  • I’m not reading from stdin, I replaced br.readLine() with getLine(t) which generates lines as I described in previous answer
  • than I added two methods myEvalLong() which evaluates using operator and operands available (simple switch) and for speed comparison I also added myEvalString(), which comparing with myEvalLong() needs more time, to parse the line part again

As you can see the amount of time used by JS eval a 50x bigger than myEvalString() and this is 4x times slower than switch…

I tried the link and expected result is 1000000003000000002, your solution returns 1000000003000000000. I hope I’ll have some time to elaborate more about the speed :wink: