adjacent Elements Product

adjacent Elements Product

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be
solution(inputArray) = 21.

7 and 3 produce the largest product.

Input/Output

 

  • [execution time limit] 4 seconds (py3)
  • [input] array.integer inputArrayAn array of integers containing at least two elements.Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10,
    -1000 ≤ inputArray[i] ≤ 1000.
  • [output] integerThe largest product of adjacent elements.

 

def solution(inputArray):
    length = len(inputArray)
    largest = inputArray[0] * inputArray[1]
   
    for i in range(0, length-1):
        product = inputArray[i] * inputArray[i+1]
        if product > largest:
            largest = product
           
    return largest
No Comments

Post A Comment