Maximum element in an array such that its previous and next element product is maximum

Given an array arr[] of N integers, the task is to print the largest element among the array such that its previous and next element product is maximum.
Examples:Â
Â
Input: arr[] = {5, 6, 4, 3, 2}Â
Output: 6Â
The product of the next and the previous elementsÂ
for every element of the given array are:Â
5 -> 2 * 6 = 12Â
6 -> 5 * 4 = 20Â
4 -> 6 * 3 = 18Â
3 -> 4 * 2 = 8Â
2 -> 3 * 5 = 15Â
Out of these 20 is the maximum.Â
Hence, 6 is the answer.
Input: arr[] = {9, 2, 3, 1, 5, 17}Â
Output: 17Â
Â
Â
Approach: For every element of the array, find the product of its previous and next element. The element which has the maximum product is the result. If two elements have an equal product of next and previous elements then choose the greater element among them.
Below is the implementation of the above approach:Â
Â
C++
#include<bits/stdc++.h>using namespace std;Â
// Function to return the largest element// such that its previous and next// element product is maximumint maxElement(int a[], int n){Â Â Â Â if (n < 3)Â Â Â Â Â Â Â Â return -1;Â
    int maxElement = a[0];    int maxProd = a[n - 1] * a[1];Â
    for (int i = 1; i < n; i++)     {Â
        // Calculate the product of the previous        // and the next element for        // the current element        int currProd = a[i - 1] * a[(i + 1) % n];Â
        // Update the maximum product        if (currProd > maxProd)         {            maxProd = currProd;            maxElement = a[i];        }Â
        // If current product is equal to the        // current maximum product then        // choose the maximum element        else if (currProd == maxProd)        {            maxElement = max(maxElement, a[i]);        }    }Â
    return maxElement;}Â
// Driver codeint main(){Â Â Â Â int a[] = { 5, 6, 4, 3, 2}; Â Â Â Â int n = sizeof(a)/sizeof(a[0]); Â Â Â Â cout << maxElement(a, n); Â Â Â Â return 0; }Â Â Â Â |
Java
// Java implementation of the approachclass GFG {Â
    // Function to return the largest element    // such that its previous and next    // element product is maximum    static int maxElement(int a[], int n)    {        if (n < 3)            return -1;Â
        int maxElement = a[0];        int maxProd = a[n - 1] * a[1];Â
        for (int i = 1; i < n; i++) {Â
            // Calculate the product of the previous            // and the next element for            // the current element            int currProd = a[i - 1] * a[(i + 1) % n];Â
            // Update the maximum product            if (currProd > maxProd) {                maxProd = currProd;                maxElement = a[i];            }Â
            // If current product is equal to the            // current maximum product then            // choose the maximum element            else if (currProd == maxProd) {                maxElement = Math.max(maxElement, a[i]);            }        }Â
        return maxElement;    }Â
    // Driver code    public static void main(String[] args)    {        int[] a = { 5, 6, 4, 3, 2 };        int n = a.length;        System.out.println(maxElement(a, n));    }} |
Python3
# Function to return the largest element# such that its previous and next# element product is maximumdef maxElement(a, n):Â
    if n < 3:        return -1    maxElement = a[0]    maxProd = a[n - 1] * a[1]Â
    for i in range(1, n):                 # Calculate the product of the previous        # and the next element for        # the current elementÂ
        currprod = a[i - 1] * a[(i + 1) % n]Â
        if currprod > maxProd:            maxProd = currprod            maxElement = a[i]                     # If current product is equal to the        # current maximum product then        # choose the maximum element        elif currprod == maxProd:            maxElement = max(maxElement, a[i])    return maxElementÂ
# Driver codeÂ
a = [5, 6, 4, 3, 2]n = len(a)#sizeof(a[0])print(maxElement(a, n))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System;Â
class GFG { Â
    // Function to return the largest element     // such that its previous and next     // element product is maximum     static int maxElement(int []a, int n)     {         if (n < 3)             return -1; Â
        int maxElement = a[0];         int maxProd = a[n - 1] * a[1]; Â
        for (int i = 1; i < n; i++)        { Â
            // Calculate the product of the previous             // and the next element for             // the current element             int currProd = a[i - 1] * a[(i + 1) % n]; Â
            // Update the maximum product             if (currProd > maxProd)             {                 maxProd = currProd;                 maxElement = a[i];             } Â
            // If current product is equal to the             // current maximum product then             // choose the maximum element             else if (currProd == maxProd)            {                 maxElement = Math.Max(maxElement, a[i]);             }         } Â
        return maxElement;     } Â
    // Driver code     public static void Main()     {         int[] a = { 5, 6, 4, 3, 2 };         int n = a.Length;         Console.WriteLine(maxElement(a, n));     } } Â
// This code is contributed by AnkitRai01 |
Javascript
<script>// Java script implementation of the approachÂ
    // Function to return the largest element    // such that its previous and next    // element product is maximum    function maxElement(a,n)    {        if (n < 3)            return -1;Â
        let maxElement = a[0];        let maxProd = a[n - 1] * a[1];Â
        for (let i = 1; i < n; i++) {Â
            // Calculate the product of the previous            // and the next element for            // the current element            let currProd = a[i - 1] * a[(i + 1) % n];Â
            // Update the maximum product            if (currProd > maxProd) {                maxProd = currProd;                maxElement = a[i];            }Â
            // If current product is equal to the            // current maximum product then            // choose the maximum element            else if (currProd == maxProd) {                maxElement = Math.max(maxElement, a[i]);            }        }Â
        return maxElement;    }Â
    // Driver code           let a = [ 5, 6, 4, 3, 2 ];        let n = a.length;        document.write(maxElement(a, n));     // This code is contributed by sravan kumar G</script> |
6
Â
Time Complexity : O(n), since there runs a loop for once from 1 to (n – 1).
Auxiliary Space : O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



