Skip to content
Advertisement

Same code but one with time limit exceeded as output and another works perfectly fine

the first code is written by me, but while executing it, I am getting Time Limit Exceeded.

JavaScript

And here is the second code, while executing, this runs perfectly fine.

JavaScript

So guys can you all tell me the difference between the time complexity of these two codes and also why is this happening?

Advertisement

Answer

The code you’re writing does an infinite loop, try running (by hand) your code and the second code with n=1

Here is a working code:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement