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 product and find the maximum product among them .
Below is the implementation of the above approach:
C++
// C++ program to print maximum product // such that its previous and next // element product is maximum. #include<bits/stdc++.h> using namespace std; // function to return largest element // such that its previous and next // element product is maximum. 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 of previous and next // element and stored into an // array product[i] product[i] = a[(i + 1) % n] * a[(i + (n - 1)) % n]; // find maximum product // in product[i] array if (maxProd < product[i]) { maxProd = product[i]; } } return maxProd; } // Driver program int main() { int a[] = { 5, 6, 4, 3, 2 }; int n = sizeof(a)/sizeof(a[0]); cout<<(maxProduct(a, n)); } // This code contributed by Rajput-Ji |
Java
// Java program to print maximum product // such that its previous and next // element product is maximum. import java.io.*; class GFG { // function to return largest element // such that its previous and next // element product is maximum. 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 of previous and next // element and stored into an // array product[i] product[i] = a[(i + 1) % n] * a[(i + (n - 1)) % n]; // find maximum product // in product[i] array if (maxProd < product[i]) { maxProd = product[i]; } } return maxProd; } // Driver code public static void main(String[] args) { int[] a = { 5, 6, 4, 3, 2 }; int n = a.length; System.out.println(maxProduct(a, n)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to print maximum product # such that its previous and next # element product is maximum. # function to return largest element # such that its previous and next # element product is maximum. def maxProduct(a, n) : product = [0]*n; maxA = [0]*n; maxProd = 0; maxArr = 0; for i in range(n) : # product of previous and next # element and stored into an # array product[i] product[i] = a[(i + 1) % n] * a[(i + (n - 1)) % n]; # find maximum product # in product[i] array if (maxProd < product[i]) : maxProd = product[i]; return maxProd; # Driver code if __name__ == "__main__" : a = [ 5, 6, 4, 3, 2 ]; n = len(a); print(maxProduct(a, n)); # This code is contributed by AnkitRai01 |
C#
// C# program to print maximum product // such that its previous and next // element product is maximum. using System; class GFG { // function to return largest element // such that its previous and next // element product is maximum. 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 of previous and next // element and stored into an // array product[i] product[i] = a[(i + 1) % n] * a[(i + (n - 1)) % n]; // find maximum product // in product[i] array if (maxProd < product[i]) { maxProd = product[i]; } } return maxProd; } // Driver code public static void Main() { int[] a = { 5, 6, 4, 3, 2 }; int n = a.Length; Console.WriteLine(maxProduct(a, n)); } } // This code is contributed by anuj_67.. |
20
Time Complexity : O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum element in an array such that its previous and next element product is maximum
- 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
- Range Query on array whose each element is XOR of index value and previous element
- Replace every element of the array by its previous element
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Maximum product of indexes of next greater on left and right
- Find next Smaller of next Greater in an array
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Replace every element of the array by its next element
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Modify given array by reducing each element by its next smaller element
- Rearrange an array such that every odd indexed element is greater than it previous
- Previous greater element
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Maximum Product Subarray | Added negative product case
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
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.

