Last active
May 4, 2021 15:08
-
-
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)
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
#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