Maximum product of an increasing subsequence of size 3 in C++ program

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] of n positive integers. Our task is to create a program to find the Maximum product of an increasing subsequence of size 3.

Problem Description − Here, we need to find the maximum product of 3 elements of the array such that they form an increasing subsequence and array index are also increasing i.e.

arr[i]*arr[j]*arr[k] is maximum,
arr[i]<arr[j]<arr[k] and i<j<k

Let’s take an example to understand the problem,

Input

arr = {5, 9, 2, 11, 4, 7}

Output

495

Explanation

All subarrays of size 3 satisfying the condition are
(5, 9, 11), prod = 5*9*11 = 495
(2, 4, 7), prod = 2*4*7 = 56
Maximum = 495

Solution Approach

A simple approach to solving the problem is looping the array and finding all subarrays of 3 that satisfy the given condition.

Find the product of elements and return the maximum of all products.

Algorithm

Initialise

maxProd = −1000

Step 1

Loop i −> 0 to n−3

Step 1.1

Loop j −> i to n−2

Step 1.1.1

if(arr[i]< arr[j]) −> loop k −> j to n−1

Step 1.1.1.1

if(arr[j] < arr[k]) −> find prod =
arr[i]*arr[j]*arr[k].

Step 1.1.1.2

if(maxProd > prod) −> maxProd = prod.

Step 2

Return maxProd.

Example

Program to illustrate the working of our solution,

 Live Demo

#include <iostream>
using namespace std;
int calcMaxProd(int arr[], int n){
   int maxProd = −1000;
   int prod;
   for (int i = 0; i < n − 2; i++)
   for (int j = i + 1; j < n − 1; j++)
   if(arr[i] < arr[j]){
      for (int k = j + 1; k < n; k++){
         if(arr[j] < arr[k]){
            prod = arr[i] * arr[j] * arr[k];
            if(maxProd < prod)
               maxProd = prod;
         }
      }
   }
   return maxProd;
}
int main(){
   int arr[] = { 5, 9, 2, 11, 4, 7 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"The maximum product of an increasing subsequence of size 3
   is "<<calcMaxProd(arr, n);
   return 0;
}

Output

The maximum product of an increasing subsequence of size 3 is 495

This solution is easy but uses 3 nested loops which makes the time complexity of the order O(n3). So, let's see an efficient solution to the problem,

In this solution, we will take elements of the array from index 1 to n−2. And treat it as the middle element of our 3 element subarray. And then find the rest two elements from the array.

Element smaller than arr[i] in array with index less than i.
Element greater than aar[i] in array with index more than i.

The smallest element is found using a self-balancing binary search tree and for the largest, we will traverse from right to left and find the maximum element in right.

After finding both values, we will find the prod of the element subarray and then find the maxProd by comparing all.

Example

Program to illustrate the working of our solution,

 Live Demo

#include<bits/stdc++.h>
using namespace std;
long calMaxSubSeqProd(int arr[] , int n) {
   int smallerLeftEle[n];
   smallerLeftEle[0] = −1 ;
   set<int>small ;
   for (int i = 0; i < n ; i++) {
      auto it = small.insert(arr[i]);
      auto val = it.first;
      −−val;
      if (val != small.end())
      smallerLeftEle[i] = *val;
      else
      smallerLeftEle[i] = −1;
   }
   long maxProd = −10000;
   long prod ;
   int greaterRightEle = arr[n−1];
   for (int i= n−2 ; i >= 1; i−−) {
      if (arr[i] > greaterRightEle)
         greaterRightEle = arr[i];
      else if (smallerLeftEle[i] != −1){
         prod = smallerLeftEle[i]*arr[i]*greaterRightEle;
         if(prod > maxProd)
            maxProd = prod;
      }
   }
   return maxProd;
}
int main() {
   int arr[] = {5, 9, 2, 11, 4, 7};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout<<"The maximum product of an increasing subsequence of size 3
   is "<<calMaxSubSeqProd(arr, n);
   return 0;
}

Output

The maximum product of an increasing subsequence of size 3 is 495
raja
Published on 09-Dec-2020 12:05:54
Advertisements