Created
May 13, 2024 07:34
-
-
Save seanhandley/c38a9b0a68570e7a7ef9bdb51d7af7aa to your computer and use it in GitHub Desktop.
Max Product
This file contains 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
MIN_LENGTH = 3 | |
def max_product(collection) | |
raise "Minimum collection size is #{MAX_LENGTH}" if collection.length < MIN_LENGTH | |
collection.sort.last(MIN_LENGTH).reduce(:*) | |
end | |
max_product([2, 4, 1, 3, -5, 6]) | |
# => 72 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment