Same solution TLE in java while AC in C++

Contest : Coders paradise
Problem : Pattern of Paradise
I am getting TLE in java solution CodeChef: Practical coding for everyone while AC in exact same solution in C++ CodeChef: Practical coding for everyone .
If anyone can tell what’s the problem.

1 Like

have a same dout bro…

Using system.out.print is gonna create overhead by switching back and through from console and thus is relatively slow. Use bufferedWriter write method which is relatively fast and does all.write operation at the end saving call to and back from.console. When using fast I/o make sure both read and write operation are fast. C++ CIN and Cout are relatively very fast compared to system.out and Scanner class.

1 Like

Yes same happened with me…Then i tried using string builder and appended ans of given test cases and display the result together. Then it worked

Oh I didn’t knew that. Thanks bro

But can you tell me the syntax for printing result in FastReader class because I took fastReader class from [Fast I/O in Java in Competitive Programming - GeeksforGeeks] . And they are using System.out.println() only.

You can either use BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
when you want to print any thing you can call output.write() method that takes a string as argument. The advantage here is that buffered writer writes in the internal buffer instead of disk, hence until buffer size is full which has a certain default value, all operations are done on internal buffer. once buffer size is full , content is written to disk, thus saving time.
once you are done writing to buffer or alternatively you are done printing, simply call output.flush() to empty the buffer and write to disk/console. thus this will save you lot of time.

Another alternative is to use StringBuilder object and append whatever you want to write to console / disk accordingly. Once you are done, you can simply call system.out.println(StringBuilder Object) and thus call to System.out.println is made only once saving execution time. . Hence you have to decide accrodingly which is the best way to go ahead based on the situation. In case you want to see how to use it you can find it in my submissions.
I hope , this will help you.

2 Likes

Got it. Thanks bro.

It’s in the title of the post. You are not using C++ and therefore are always more likely to get TLE, where the likelihood will be problem specific.

What do you mean? Should I stop using java in competitive coding??

There are different time limit for different languages . Codechef judge has twice the time limit for Java compared to C++. Most preferred language of programmers is C++ in competitive coding. Every programming language has its pros and cons , C and C++ are the fastest language among high level languages followed by Java. You can continue competitive coding with Java. Competitive coder - Petr has used Java as primary language and he is among top 5 on codeforces and this shows that language won’t be a barrier. Knowing more than one language is always helpful.

3 Likes

You don’t need to stop using Java. But you need to stop having the same performance expectations as C++.

There are N number of reasons why for every 100 codes in c++, 5 of them may TLE in java, 10 in python and so on. But it would be very rare the other way around, (even after adjusting for the language speed factor in Codechef, which you won’t get on other websites). These reasons include the language itself, as well as easy availability of good implementations of standard algorithms.

I don’t mean to say you can’t become 6 star or 7 star using Java or Python, but c++ users do have an advantage in competitive programming. As mentioned above, knowing more than 1 language is always helpful as a backup. I personally like to work with Python a lot and use c++ on occasions that I get a TLE in Python, where I am sure of the asymptotic complexity being good enough and I often get AC with that (happens in every long challenge to me)

3 Likes

Accepted solution in java [CodeChef: Practical coding for everyone] .
Apparently System.out.println() is very slow that’s why it was giving TLE. I just replaced it with output.write() and flushed the answer at end and it got accepted.

Thanks a lot Man, you made my day.