nlogn solution to the make_free_k problem in Ruby