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;
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++)
{
int currProd = a[i - 1] * a[(i + 1) % n];
if (currProd > maxProd)
{
maxProd = currProd;
maxElement = a[i];
}
else if (currProd == maxProd)
{
maxElement = max(maxElement, a[i]);
}
}
return maxElement;
}
int main()
{
int a[] = { 5, 6, 4, 3, 2};
int n = sizeof (a)/ sizeof (a[0]);
cout << maxElement(a, n);
return 0;
}
|
Java
class GFG {
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++) {
int currProd = a[i - 1 ] * a[(i + 1 ) % n];
if (currProd > maxProd) {
maxProd = currProd;
maxElement = a[i];
}
else if (currProd == maxProd) {
maxElement = Math.max(maxElement, a[i]);
}
}
return maxElement;
}
public static void main(String[] args)
{
int [] a = { 5 , 6 , 4 , 3 , 2 };
int n = a.length;
System.out.println(maxElement(a, n));
}
}
|
Python3
def maxElement(a, n):
if n < 3 :
return - 1
maxElement = a[ 0 ]
maxProd = a[n - 1 ] * a[ 1 ]
for i in range ( 1 , n):
currprod = a[i - 1 ] * a[(i + 1 ) % n]
if currprod > maxProd:
maxProd = currprod
maxElement = a[i]
elif currprod = = maxProd:
maxElement = max (maxElement, a[i])
return maxElement
a = [ 5 , 6 , 4 , 3 , 2 ]
n = len (a)
print (maxElement(a, n))
|
C#
using System;
class GFG
{
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++)
{
int currProd = a[i - 1] * a[(i + 1) % n];
if (currProd > maxProd)
{
maxProd = currProd;
maxElement = a[i];
}
else if (currProd == maxProd)
{
maxElement = Math.Max(maxElement, a[i]);
}
}
return maxElement;
}
public static void Main()
{
int [] a = { 5, 6, 4, 3, 2 };
int n = a.Length;
Console.WriteLine(maxElement(a, n));
}
}
|
Javascript
<script>
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++) {
let currProd = a[i - 1] * a[(i + 1) % n];
if (currProd > maxProd) {
maxProd = currProd;
maxElement = a[i];
}
else if (currProd == maxProd) {
maxElement = Math.max(maxElement, a[i]);
}
}
return maxElement;
}
let a = [ 5, 6, 4, 3, 2 ];
let n = a.length;
document.write(maxElement(a, n));
</script>
|
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.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...