Skip to content

Instantly share code, notes, and snippets.

@JosephDunivan
Last active May 4, 2021 15:08
Show Gist options
  • Save JosephDunivan/e2a9ff52cb7dcd8e690125070d8bc9c9 to your computer and use it in GitHub Desktop.
Save JosephDunivan/e2a9ff52cb7dcd8e690125070d8bc9c9 to your computer and use it in GitHub Desktop.
Ruby finds the largest product of two adjacent numbers in an array in linear time O(1)
#finds the largest product of two adjacent numbers in an array
def adjacentElementsProduct(inputArray)
inputArray.each_cons(2).map { |x, y| x*y }.max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment