Maximum element in an array such that its previous and next element product is maximum | Function to return the largest element such that its previous and next element product is maximum ; Calculate the product of the previous and the next element for the current element ; Update the maximum product ; If current product is equal to the current maximum product then choose the maximum element ; Driver code
def maxElement ( a , n ) : NEW_LINE INDENT if n < 3 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT maxElement = a [ 0 ] NEW_LINE maxProd = a [ n - 1 ] * a [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT currprod = a [ i - 1 ] * a [ ( i + 1 ) % n ] NEW_LINE if currprod > maxProd : NEW_LINE INDENT maxProd = currprod NEW_LINE maxElement = a [ i ] NEW_LINE DEDENT elif currprod == maxProd : NEW_LINE INDENT maxElement = max ( maxElement , a [ i ] ) NEW_LINE DEDENT DEDENT return maxElement NEW_LINE DEDENT a = [ 5 , 6 , 4 , 3 , 2 ] NEW_LINE n = len ( a ) sizeof ( a [ 0 ] ) NEW_LINE print ( maxElement ( a , n ) ) NEW_LINE