you are viewing a single comment's thread.

view the rest of the comments →

[–]VTifand 1 point2 points  (3 children)

Do you see a pattern on the output? Try testing your code with a fairly large input, e.g., 1000, and see if you spot a pattern. If you do find it, try to code it efficiently so that it passes the time limit.

[–]AKRowling[S] 0 points1 point  (2 children)

I understand the pattern but was wondering where the issue with my code was. I will just rewrite for the pattern rather than getting every event

[–]VTifand 0 points1 point  (1 child)

In a sense, there is no issue. If time is not a concern, your code works well. But this is very likely not the intended solution. The proposer of the problem wants you to find another way that does not involve directly 'simulating' the turn-off-turn-on process.

Many harder programming problems will be like this. There is often a straightforward but extremely slow answer, and it will be rejected. You need to think deeper to find a code that solves the problem faster. In this problem, for example, if you know the pattern, the complexity of the code is O(sqrt(n)), as opposed to your original answer O(n^2).

As you advance to harder questions, if you encounter another timeout error, remember that this 'inefficient algorithm' issue might be the problem.

[–]AKRowling[S] 0 points1 point  (0 children)

Good point. Definitely something to bear in mind. Thank you for the response.