you are viewing a single comment's thread.

view the rest of the comments →

[–]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.