Ad
  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    @Beaxx, I don't think he is being rude, specially because ORION doesn't seem to be addressing the person who posted this solution but the community who voted it as clever and best practice, and the community does deserve the rough talk for voting this as best practice and clever when it is needlessly slower while not having any pros over other solutions. And quite honestly, the fact that happens is really disappointing for more knowledgable programmers.

    As for his critic, while lacking in detail, it is accurate - calling sum twice per iteration of the for loop is innefficient if it can be avoided. Sum will iterate over all the items passed to it as a parameter internally so you basically have two for loops inside another for loop (this is exponetial time). In this case, it gets the job done, but is not a best practice because it can be easily avoided by keeping track of the sums yourself as you iterate over th array (e.g. my solution, and I am pretty sure you will find others that do it at least as well or better).