Maximum product of indexes of next greater on left and right in C++ Program

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[]. Our task is to create a program to calculate the Maximum product of indexes of next greater on left and right.

Problem Description

For the given array we need to find the product of maximum value of left[i]*right[i]. Both arrays are defined as −

left[i] = j, such that arr[i] <’. ‘ arr[j] and i > j.
right[i] = j, such that arr[i] < arr[j] and i < j.
*The array is 1 indexed.

Let’s take an example to understand the problem,

Input

arr[6] = {5, 2, 3, 1, 8, 6}

Output

15

Explanation

Creating left array,
left[] = {0, 1, 1, 3, 0, 5}
right[] = {5, 3, 5, 5, 0, 0}
Index products :
1 −> 0*5 = 0
2 −> 1*3 = 3
3 −> 1*5 = 5
4 −> 3*5 = 15
5 −> 0*0 = 0
6 −> 0*5 = 0

Maximum product

15

Solution Approach

To find the maximum product of the index of a greater element on the left and right of the element. We will first find the indexes of left and right greater and store their product for comparison.

Now, to find the greatest element of left and right, we will one by one check for greater elements in indexes value on left and right. To find we will use the stack. And do the following operations,

If stack is empty −> push current index, tos = 1. Tos is top of the stack
Else if arr[i] > arr[tos] −> tos = 1.

Using this we can find index values of all elements greater than the given element of left and right of the array.

Example

Program to illustrate the working of our solution,

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int* findNextGreaterIndex(int a[], int n, char ch ) {
   int* greaterIndex = new int [n];
   stack<int> index;
   if(ch == 'R'){
      for (int i = 0; i < n; ++i) {
         while (!index.empty() && a[i] > a[index.top() − 1]) {
            int indexVal = index.top();
            index.pop();
            greaterIndex[indexVal − 1] = i + 1;
         }
         index.push(i + 1);
      }
   }
   else if(ch == 'L'){
      for (int i = n − 1; i >= 0; i−−) {
         while (!index.empty() && a[i] > a[index.top() − 1]) {
            int indexVal = index.top();
            index.pop();
            greaterIndex[indexVal − 1] = i + 1;
         }
         index.push(i + 1);
      }
   }
   return greaterIndex;
}
int calcMaxGreaterIndedxProd(int arr[], int n) {
   int* left = findNextGreaterIndex(arr, n, 'L');
   int* right = findNextGreaterIndex(arr, n, 'R');
   int maxProd = −1000;
   int prod;
   for (int i = 1; i < n; i++) {
      prod = left[i]*right[i];
      if(prod > maxProd)
         maxProd = prod;
   }
   return maxProd;
}
int main() {
   int arr[] = { 5, 2, 3, 1, 8, 6};
   int n = sizeof(arr) / sizeof(arr[1]);
   cout<<"The maximum product of indexes of next greater on left and
   right is "<<calcMaxGreaterIndedxProd(arr, n);
   return 0;
}

Output

The maximum product of indexes of next greater on left and right is 15
raja
Published on 09-Dec-2020 12:11:33
Advertisements