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 maximum ` `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 = max(maxElement, a[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `maxElement; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 5, 6, 4, 3, 2}; ` ` ` `int` `n = ` `sizeof` `(a)/` `sizeof` `(a[0]); ` ` ` `cout << maxElement(a, n); ` ` ` `return` `0; ` `} ` ` ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `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(String[] args) ` ` ` `{ ` ` ` `int` `[] a = { ` `5` `, ` `6` `, ` `4` `, ` `3` `, ` `2` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(maxElement(a, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Function to return the largest element ` `# such that its previous and next ` `# element product is maximum ` `def` `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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum previous and next element product
- Replace every array element by Bitwise Xor of previous and next element
- Generate an array having Bitwise AND of the previous and the next element
- Replace every array element by multiplication of previous and next
- Replace every array element by sum of previous and next
- Elements greater than the previous and next element in an Array
- Replace every element of the array by its previous element
- Count of Array elements greater than all elements on its left and next K elements on its right
- Replace every element of the array by its next element
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Rearrange an array such that every odd indexed element is greater than it previous
- Range Query on array whose each element is XOR of index value and previous element
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find next Smaller of next Greater in an array
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Maximum product of indexes of next greater on left and right
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Find Array formed by adding each element of given array with largest element in new array to its left
- Number of full binary trees such that each node is product of its children

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.