Ad
  • Default User Avatar

    @snake2

    Assume that there is no match for the midpoint, so you'd return -1.

    arr.each_index do |i| will iterate n times (where n is the length of arr).
    Inside each iteration, the reduce call is itself iterating m times (0...i) and l times (i+1..-1) [l + m = n - 1, though, which is effectively n].
    So you are doing n times n (or n2) iterations, so you have a function that costs O(n2) to run.

    This can be solved with a cost of O(n). Someone smarter than me might have one even better. Maybe.

  • Default User Avatar

    what makes it not eff? also what of my code? its a little similar, but im just a beginner.