How to overcome timelimit in c language? | SoloLearn: Learn to code for FREE!

+8

How to overcome timelimit in c language?

I need 1000 loop but that makes my code have timelimit problem

11/20/2020 6:18:29 PM

Briana

25 Answers

New Answer

+7

Briana well then you can solve the problem by making your function working only through iteration

+13

There isn't any limit of 1000 loops. It's not about the loop count as much as it's about what you're doing in each loop. Notice that this very simple Code Bit can loop 200 million times without timing out. https://code.sololearn.com/cKk2O52voasj/?ref=app

+6

Briana every time you call the function it calls itself 2 times, in each of them it cals itself 2 more time, and so on till you reach j(1) and j(0). So when you call j(40) just one time you are actually calling the function 2^40 times that means 1 099 500 000 000 times. If you want to overcome the time limit you have to forget about recursion. To use iteration make an array that start with 2 and 1 and then with a loop you mke all the following subscripts equal to the sum of the previous two plus 1.

+3

1000 laps are not that much. What are you doing in the loop? Are you calling a recursive function?

+3

Davide uh.. Okay. I'll try, thankyou

+2

Your welcome 🤗

+2

Briana you are welcome 🤗

+1

That's just a limitation if sololearn. You can run it on a PC without limitation

+1

Davide yes..

+1

Benjamin Jürgens no.. It's timelimit in online judge

+1

If you post your code you might get some suggestions how to optimize it. I don't know online judge, but every online code execution service will have limitations like that

+1

Briana Can you post the code. That would help everyone a lot.

+1

Aradhay Mathur this https://code.sololearn.com/cLBcN551QI1Y/?ref=app When i put any number of testcase and i put like 30-40 for case 1 (example), the output will out so long time

+1

Briana Can you explain what you want as the output because I can understand the code.

+1

Briana are you trying to calculate fibonacci numbers? Then have a look at other codes for reference https://code.sololearn.com/cw4lQtpo1ke1/?ref=app https://code.sololearn.com/c6xOYOCrd7FU/?ref=app https://code.sololearn.com/cc6zpO6KQAZx/?ref=app

+1

Benjamin Jürgens no.. it's own formulas. I only want to overcome the timelimit error

+1

Ok, so it is really just the timeout problem. You are recursing to the botton (n0 and n1) every time and since the code uses recursion twice, the effort doubles from n to n+1 Use an array J to save all known values. Use recursion only if the needed value is not in the array

+1

Some small general advices: - Use proper indention - Don't suppress warnings like Aradhay suggested, they tell you valuable information and actually help you - Don't implement the limits into your code, they are just for you to know what to expect The warning says your function j does not return anything in some cases (n>40). Remove the condition on the last else branch

+1

Davide i already tried it. It's work with iteration. Thankyou so much

+1

For your case, use simple iteration and memoization.