Last active
February 14, 2018 11:34
-
-
Save caius/f31dd5e07bfcd732647d2d90ebcc9aa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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