Maximum product of indexes of next greater on left and right

Given an array a[1..N]. For each element at position i (1 <= i <= N). Where

  1. 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.
  2. 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.

Output:

8


My Personal Notes arrow_drop_up

Recommended Posts:


    Article Tags :
    Practice Tags :



    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.