Skip to content

Instantly share code, notes, and snippets.

@caius
Last active February 14, 2018 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caius/f31dd5e07bfcd732647d2d90ebcc9aa8 to your computer and use it in GitHub Desktop.
Save caius/f31dd5e07bfcd732647d2d90ebcc9aa8 to your computer and use it in GitHub Desktop.
# Given an input array, find the longest subarray within it (without sort) that contains numbers in ascending order
input = [12, 4, 78, 90, 45, 23]
out = input.length.downto(1) do |i|
subarr = input.each_cons(i).find do |arr|
arr.each_cons(2).map { |a, b| a < b }.uniq == [true]
end
break(subarr) if subarr
end
out # => [4, 78, 90]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment