Ad
  • Default User Avatar

    Good for readability. Took 2300 ms for 10000000 runs

  • Default User Avatar

    I believe this works, but it's very not-effecient solution

  • Custom User Avatar

    not to mention many other solutions are O(n) but this is (n^2)...

  • Default User Avatar

    I could be totally wrong here, I'm a bit of a newb, but is this really deserving of all the best practice votes? Creating 2 separate methods for what could easily be done with a couple for loops inside of findEvenIndex() just feels overly complicated.

  • Default User Avatar

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

  • Default User Avatar

    Actually, you can do this if you know dynamic programming.

  • Default User Avatar

    I blame YouTube for that.
    Hence mob-upvoting the refactoring of a brute force solution into a method that replicates the behavior of native Java, except with higher CPU overhead.
    This site offers a wonderful chance to actually learn a new language and use everything it has to offer, but not everyone is willing to take on the challenge.
    I say they're missing out, but to each their own.

  • Custom User Avatar

    Best pratices doesn't mean most faster or efficient. Good balance (performance, readability, maintainability) is the key of best pratices: so it includes good readability.

  • Custom User Avatar

    As for why so many people marked it as "Best Practice"... maybe they liked the variable names and spacing between lines?! Just kidding. I'm guessing they just didn't realize there was a better way. Or they exhibited mob behaviour - once it has a few upvotes others upvote to go with the crowd.

  • Custom User Avatar

    @sheriv, it's not one iteration. For each index he's checking he sums up all the numbers on the left and all the numbers on the right. He's forcing the CPU to do the same calculations over and over, and to solve this it's unnecessary. For example if you are at an index and already know the left and right sums at that point, you only need to remove one element and add one element to check the neighbouring index - but the solution above has the program recalculate the entire sum on both sides. It's functional but it's not elegant or efficient.

  • Custom User Avatar

    I think the best solution gives us O(n) complexity since there'is only one iteration.

  • Default User Avatar

    Not even easy to read. Just bad solution... ;)

  • Default User Avatar

    I'm always wondering why people consider solutions like this best practice. Easy to read and understand: Yes. Best practice: Never ever.

  • Default User Avatar

    Running time of this solution is quadratic O(n^2), where n is length of array. Functions that iterate through the array are called for each element in the array. At the same time, this problem could be solved in a linear time O(2n). So, I would like to ask: why do we see Best Practices points for this solution? This is a brute force solution, it may be good as an attempt or for testing purposes, but why "Best Practice"?

    I tried to execute this:


    int[] arr = new int[100000001];

    Arrays.fill(arr, 20);

    findEvenIndex(arr);



    Linear time solution gives answer in just 1 second on my computer, this quadratic solution executes forever (I waited for several minutes and stopped it), with input array of length 100000 elements, this solution gives answer in 12 seconds.