Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This comment is hidden because it contains spoiler information about the solution
Neat!
This comment is hidden because it contains spoiler information about the solution
@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 ofarr
).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.
what makes it not eff? also what of my code? its a little similar, but im just a beginner.
So cool!
This comment is hidden because it contains spoiler information about the solution
Well, I mean, the instructions say to return a variable 'l', and it's part of the code that's automatically put into the solution. I know my comment seems pedantic, but that wasn't my intention. I really do think this is the best answer, just that it doesn't follow that specific instruction.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Neither efficient or readable