Given an array a[1..N]. For each element at position i (1 <= i <= N). Where
- L(i) is defined as closest index j such that j < i and a[j] > a[i]. If no such j exists then L(i) = 0.
- R(i) is defined as closest index k such that k > i and a[k] > a[i]. If no such k exists then R(i) = 0.
LRProduct(i) = L(i)*R(i) .
We need to find an index with maximum LRProduct
Examples:
Input : 1 1 1 1 0 1 1 1 1 1
Output : 24
For {1, 1, 1, 1, 0, 1, 1, 1, 1, 1} all element are same except 0. So only for zero their exist greater element and for others it will be zero. for zero, on left 4th element is closest and greater than zero and on right 6th element is closest and greater. so maximum
product will be 4*6 = 24.
Input : 5 4 3 4 5
Output : 8
For {5, 4, 3, 4, 5}, L[] = {0, 1, 2, 1, 0} and R[]
= {0, 5, 4, 5, 0},
LRProduct = {0, 5, 8, 5, 0} and max in this is 8.
Note: Taking starting index as 1 for finding LRproduct.
This problem is based on Next Greater Element.
From the current position, we need to find the closest greater element on its left and right side.
So to find next greater element, we used stack one from left and one from right.simply we are checking which element is greater and storing their index at specified position.
1- if stack is empty, push current index.
2- if stack is not empty
….a) if current element is greater than top element then store the index of current element on index of top element.
Do this, once traversing array element from left and once from right and form the left and right array, then, multiply them to find max product value.
C++
// C++ program to find the max // LRproduct[i] among all i #include <bits/stdc++.h> using namespace std; #define MAX 1000 // function to find just next greater // element in left side vector<int> nextGreaterInLeft(int a[], int n) { vector<int> left_index(MAX, 0); stack<int> s; for (int i = n - 1; i >= 0; i--) { // checking if current element is greater than top while (!s.empty() && a[i] > a[s.top() - 1]) { int r = s.top(); s.pop(); // on index of top store the current element // index which is just greater than top element left_index[r - 1] = i + 1; } // else push the current element in stack s.push(i + 1); } return left_index; } // function to find just next greater element // in right side vector<int> nextGreaterInRight(int a[], int n) { vector<int> right_index(MAX, 0); stack<int> s; for (int i = 0; i < n; ++i) { // checking if current element is greater than top while (!s.empty() && a[i] > a[s.top() - 1]) { int r = s.top(); s.pop(); // on index of top store the current element // index which is just greater than top element // stored index should be start with 1 right_index[r - 1] = i + 1; } // else push the current element in stack s.push(i + 1); } return right_index; } // Function to find maximum LR product int LRProduct(int arr[], int n) { // for each element storing the index of just // greater element in left side vector<int> left = nextGreaterInLeft(arr, n); // for each element storing the index of just // greater element in right side vector<int> right = nextGreaterInRight(arr, n); int ans = -1; for (int i = 1; i <= n; i++) { // finding the max index product ans = max(ans, left[i] * right[i]); } return ans; } // Drivers code int main() { int arr[] = { 5, 4, 3, 4, 5 }; int n = sizeof(arr) / sizeof(arr[1]); cout << LRProduct(arr, n); return 0; } |
Java
// Java program to find the // max LRproduct[i] among all i import java.io.*; import java.util.*; class GFG { static int MAX = 1000; // function to find just next // greater element in left side static int[] nextGreaterInLeft(int []a, int n) { int []left_index = new int[MAX]; Stack<Integer> s = new Stack<Integer>(); for (int i = n - 1; i >= 0; i--) { // checking if current // element is greater than top while (s.size() != 0 && a[i] > a[s.peek() - 1]) { int r = s.peek(); s.pop(); // on index of top store // the current element // index which is just // greater than top element left_index[r - 1] = i + 1; } // else push the current // element in stack s.push(i + 1); } return left_index; } // function to find just next // greater element in right side static int[] nextGreaterInRight(int []a, int n) { int []right_index = new int[MAX]; Stack<Integer> s = new Stack<Integer>(); for (int i = 0; i < n; ++i) { // checking if current element // is greater than top while (s.size() != 0 && a[i] > a[s.peek() - 1]) { int r = s.peek(); s.pop(); // on index of top store // the current element index // which is just greater than // top element stored index // should be start with 1 right_index[r - 1] = i + 1; } // else push the current // element in stack s.push(i + 1); } return right_index; } // Function to find // maximum LR product static int LRProduct(int []arr, int n) { // for each element storing // the index of just greater // element in left side int []left = nextGreaterInLeft(arr, n); // for each element storing // the index of just greater // element in right side int []right = nextGreaterInRight(arr, n); int ans = -1; for (int i = 1; i <= n; i++) { // finding the max // index product ans = Math.max(ans, left[i] * right[i]); } return ans; } // Driver code public static void main(String args[]) { int []arr = new int[]{ 5, 4, 3, 4, 5 }; int n = arr.length; System.out.print(LRProduct(arr, n)); } } // This code is contributed by // Manish Shaw(manishshaw1) |
Python3
# Python3 program to find the # max LRproduct[i] among all i # Method to find the next greater # value in left side def nextGreaterInLeft(a): left_index = [0] * len(a) s = [] for i in range(len(a)): # Checking if current # element is greater than top while len(s) != 0 and a[i] >= a[s[-1]]: # Pop the element till we can't # get the larger value then # the current value s.pop() if len(s) != 0: left_index[i] = s[-1] else: left_index[i] = 0 # Else push the element in the stack s.append(i) return left_index # Method to find the next # greater value in right def nextGreaterInRight(a): right_index = [0] * len(a) s = [] for i in range(len(a) - 1, -1, -1): # Checking if current element # is greater than top while len(s) != 0 and a[i] >= a[s[-1]]: # Pop the element till we can't # get the larger value then # the current value s.pop() if len(s) != 0: right_index[i] = s[-1] else: right_index[i] = 0 # Else push the element in the stack s.append(i) return right_index def LRProduct(arr): # For each element storing # the index of just greater # element in left side left = nextGreaterInLeft(arr) # For each element storing # the index of just greater # element in right side right = nextGreaterInRight(arr) ans = -1 # As we know the answer will # belong to the range from # 1st index to second last index. # Because for 1st index left # will be 0 and for last # index right will be 0 for i in range(1, len(left) - 1): if left[i] == 0 or right[i] == 0: # Finding the max index product ans = max(ans, 0) else: temp = (left[i] + 1) * (right[i] + 1) # Finding the max index product ans = max(ans, temp) return ans # Driver Code arr = [ 5, 4, 3, 4, 5 ] print(LRProduct(arr)) # This code is contributed by Mohit Pathneja |
C#
// C# program to find the max LRproduct[i] // among all i using System; using System.Collections.Generic; class GFG { static int MAX = 1000; // function to find just next greater // element in left side static int[] nextGreaterInLeft(int []a, int n) { int []left_index = new int[MAX]; Stack<int> s = new Stack<int>(); for (int i = n - 1; i >= 0; i--) { // checking if current element is // greater than top while (s.Count != 0 && a[i] > a[s.Peek() - 1]) { int r = s.Peek(); s.Pop(); // on index of top store the current // element index which is just greater // than top element left_index[r - 1] = i + 1; } // else push the current element in stack s.Push(i + 1); } return left_index; } // function to find just next greater element // in right side static int[] nextGreaterInRight(int []a, int n) { int []right_index = new int[MAX]; Stack<int> s = new Stack<int>(); for (int i = 0; i < n; ++i) { // checking if current element is // greater than top while (s.Count != 0 && a[i] > a[s.Peek() - 1]) { int r = s.Peek(); s.Pop(); // on index of top store the current // element index which is just greater // than top element stored index should // be start with 1 right_index[r - 1] = i + 1; } // else push the current element in stack s.Push(i + 1); } return right_index; } // Function to find maximum LR product static int LRProduct(int []arr, int n) { // for each element storing the index of just // greater element in left side int []left = nextGreaterInLeft(arr, n); // for each element storing the index of just // greater element in right side int []right = nextGreaterInRight(arr, n); int ans = -1; for (int i = 1; i <= n; i++) { // finding the max index product ans = Math.Max(ans, left[i] * right[i]); } return ans; } // Drivers code static void Main() { int []arr = new int[]{ 5, 4, 3, 4, 5 }; int n = arr.Length; Console.Write(LRProduct(arr, n)); } } // This code is contributed by Manish Shaw |
8
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:
- Count of Array elements greater than all elements on its left and next K elements on its right
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Find next Smaller of next Greater in an array
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Maximum element in an array such that its previous and next element product is maximum
- Maximize trailing zeros in product from top left to bottom right of given Matrix
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Find maximum difference between nearest left and right smaller elements
- Maximum difference between first and last indexes of an element in array
- Maximum previous and next element product
- Elements greater than the previous and next element in an Array
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Queries on Left and Right Circular shift on array
- Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find the difference of count of equal elements on the right and the left for each element
- Replace elements with absolute difference of smallest element on left and largest element on right
- Find Partition Line such that sum of values on left and right is equal
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.

