Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Created May 13, 2024 07:34
Show Gist options
  • Save seanhandley/c38a9b0a68570e7a7ef9bdb51d7af7aa to your computer and use it in GitHub Desktop.
Save seanhandley/c38a9b0a68570e7a7ef9bdb51d7af7aa to your computer and use it in GitHub Desktop.
Max Product
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