'Ruby - There's an array of integers given. I have to find the pair of adjacent elements that has the largest product
This little thing in the arcade mode of codesignal.com is literally killing me.
Here's the problem-
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. For inputArray = [3, 6, -2, -5, 7, 3], the output should be adjacentElementsProduct(inputArray) = 21. 7 and 3 produce the largest product.
This is the code I could manage:
def adjacentElementsProduct(inputArray)
for check in 0..(inputArray.length-1)
previous = -5000000000000000000000000000000
if check < inputArray.length-1
name = inputArray[check] * inputArray[check+1]
chosen = name if name > previous
end
end
return chosen
end
This gives 3/9 correct answers.
Solution 1:[1]
def max_product_adjacent_elements(arr)
arr.each_cons(2).max_by { |x,y| x*y }.reduce(:*)
end
arr = (-50..100).to_a.sample(10)
#=> [100, 0, 4, 20, -45, 71, 21, 39, 40, -33]
max_product_adjacent_elements(arr)
#=> 1560 (39*40)
See Enumerable#each_cons and Enumerable#max_by.
Here's another way.
mx = -Float::INFINITY
enum = arr.to_enum
#=> #<Enumerator: [63, 76, 31, 25, 38, 91, 87, 34, 86, 72]:each>
loop { mx = [mx, enum.next * enum.peek].max }
mx
#=> 1560
When the enumerator is at its last position in arr, Enumerator#peek raises a StopIteration exception that Kernel#loop handles by breaking out of the loop.
Solution 2:[2]
Just in case somebody would consider first and last elements as adjacent:
def max_product_adjacent_elements_circular(arr)
prod = 0
arr.size.times do
new_prod = arr.rotate![0..1].reduce(:*)
prod = new_prod if new_prod > prod
end
prod
end
Solution 3:[3]
arr.each_cons(2).max_by{|x, y| x*y }.inject(:*)
each_cons(N)function takes the block which is used to check the condition and N which specifies the number of consecutive elements to take.- follow by
maxwhich will return the maximum pair outcome inject(:*)multiply the two
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | iGian |
| Solution 3 |
