Algorithm help

i never thought i'd get any help at all on this and ended up
with so many
viable ideas... what a great list!

Yeah, and I hope this is the last message (at least my last
message) in this thread :slight_smile:

def to_ranges d
    return unless d && d.size > 0
    return [d.first..d.last] if (d.last - d.first).abs==d.size-1
    r1, r2 = to_ranges(d[0...(d.size/2)]), to_ranges(d[(d.size/2)..-1])
    return r1+r2 if (r1.last.last - r2.first.first).abs != 1
    r1[0..-2]+[r1.last.first..r2.first.last]+r2[1..-1]
end

That's not so short but it handles negativ values, reversed arrays
and empty arrays and all of them in O(log n) time.

I hope that's it.

Simon