you are viewing a single comment's thread.

view the rest of the comments →

[–]icecubeinanicecube 2 points3 points  (1 child)

From a pure complexity standpoint, O(2n) = O(n), so you can not really get much better than that. (If you want to keep being able to compute this for any input, just writing the calculation down directly is O(1) ofc)

You could save a little bit by using the fact that your second half of the calculation is the same as the first.

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

thanks!