Maximum previous and next element product
Given an array of integers, Task is to print the Maximum Product among the array such that its previous and next element product is maximum.
Note: Array can be considered in the cyclic order. The previous element of the first element would be equal to the last element and the next element for the last element would be the first element.
Examples:
Input : a[ ] = { 5, 6, 4, 3, 2}
Output : 20
For 5 :previous element is 2 and next element is 6 so, product will be 12.
For 6 :previous element is 5 and next element is 4 so, product will be 20.
For 4 :previous element is 6 and next element is 3 so, product will be 18.
For 3 :previous element is 4 and next element is 2 so, product will be 8.
For 2 :previous element is 3 and next element is 5 so, product will be 15.
maximum possible product is 20
and maximum element in an array is 6.
Input : a[ ] = {9, 2, 3, 1, 5, 17}
Output : 45
Approach:
- Idea is to firstly find previous element and next element.
- After finding both element, take the product and find the maximum product among them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProduct( int a[], int n)
{
int product[n];
int maxA[n];
int maxProd = 0;
int maxArr = 0;
for ( int i = 0; i < n; i++) {
product[i] = a[(i + 1) % n] * a[(i + (n - 1)) % n];
if (maxProd < product[i]) {
maxProd = product[i];
}
}
return maxProd;
}
int main()
{
int a[] = { 5, 6, 4, 3, 2 };
int n = sizeof (a) / sizeof (a[0]);
cout << (maxProduct(a, n));
}
|
Java
import java.io.*;
class GFG
{
static int maxProduct( int a[], int n)
{
int [] product = new int [n];
int maxA[] = new int [n];
int maxProd = 0 ;
int maxArr = 0 ;
for ( int i = 0 ; i < n; i++)
{
product[i] = a[(i + 1 ) % n] *
a[(i + (n - 1 )) % n];
if (maxProd < product[i])
{
maxProd = product[i];
}
}
return maxProd;
}
public static void main(String[] args)
{
int [] a = { 5 , 6 , 4 , 3 , 2 };
int n = a.length;
System.out.println(maxProduct(a, n));
}
}
|
Python3
def maxProduct(a, n) :
product = [ 0 ] * n;
maxA = [ 0 ] * n;
maxProd = 0 ;
maxArr = 0 ;
for i in range (n) :
product[i] = a[(i + 1 ) % n] * a[(i + (n - 1 )) % n];
if (maxProd < product[i]) :
maxProd = product[i];
return maxProd;
if __name__ = = "__main__" :
a = [ 5 , 6 , 4 , 3 , 2 ];
n = len (a);
print (maxProduct(a, n));
|
C#
using System;
class GFG
{
static int maxProduct( int []a, int n)
{
int [] product = new int [n];
int maxProd = 0;
for ( int i = 0; i < n; i++)
{
product[i] = a[(i + 1) % n] *
a[(i + (n - 1)) % n];
if (maxProd < product[i])
{
maxProd = product[i];
}
}
return maxProd;
}
public static void Main()
{
int [] a = { 5, 6, 4, 3, 2 };
int n = a.Length;
Console.WriteLine(maxProduct(a, n));
}
}
|
Javascript
<script>
function maxProduct(a, n)
{
let product = new Array(n);
let maxProd = 0;
for (let i = 0; i < n; i++)
{
product[i] = a[(i + 1) % n] *
a[(i + (n - 1)) % n];
if (maxProd < product[i])
{
maxProd = product[i];
}
}
return maxProd;
}
let a = [ 5, 6, 4, 3, 2 ];
let n = a.length;
document.write(maxProduct(a, n));
</script>
|
Time Complexity : O(N) where N is size of given array
Auxiliary space: O(N) because it is using extra space for arrays product and maxA
Last Updated :
13 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...