
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum product of an increasing subsequence in C++ Program
In this problem, we are given an array arr[] of size n. Our task is to find the maximum product of an increasing subsequence.
Problem Description − We need to find the maximum product of increasing subsequence of any size possible from the elements of the array.
Let’s take an example to understand the problem,
Input
arr[] = {5, 4, 6, 8, 7, 9}
Output
2160
Explanation
All Increasing subsequence: {5,6,8,9}. Prod = 2160 {5,6,7,9}. Prod = 1890 Here, we have considered only max size subsequence.
Solution Approach
A simple solution to the problem is by using a dynamic programming approach. For this, we will store the maximum product increasing subsequence till the given element of the array and then store in an array.
Algorithm
Initialise −
prod[] with elements of arr[]. maxProd = −1000
Step 1 −
Loop for i −> 0 to n−1
Step 1.1 −
Loop for i −> 0 to i
Step 1.1.1
Check if the current element creates an increasing subsequence i.e. arr[i]>arr[j] and arr[i]*prod[j]> prod[i] −> prod[i] = prod[j]*arr[i]
Step 2 −
find the maximum element of the array. Following steps 3 and 4.
Step 3 −
Loop form i −> 0 to n−1
Step 4 −
if(prod[i] > maxProd) −> maxPord = prod[i]
Step 5 −
return maxProd.
Example
Program to show the implementation of our solution,
#include <iostream> using namespace std; long calcMaxProdSubSeq(long arr[], int n) { long maxProdSubSeq[n]; for (int i = 0; i < n; i++) maxProdSubSeq[i] = arr[i]; for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) if (arr[i] > arr[j] && maxProdSubSeq[i] < (maxProdSubSeq[j] * arr[i])) maxProdSubSeq[i] = maxProdSubSeq[j] * arr[i]; long maxProd = −1000 ; for(int i = 0; i < n; i++){ if(maxProd < maxProdSubSeq[i]) maxProd = maxProdSubSeq[i]; } return maxProd; } int main() { long arr[] = {5, 4, 6, 8, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The maximum product of an increasing subsequence is "<<calcMaxProdSubSeq(arr, n); return 0; }
Output
The maximum product of an increasing subsequence is 2160
- Related Articles
- Maximum product of an increasing subsequence in C++
- Maximum product of an increasing subsequence of size 3 in C++ program
- Maximum product of an increasing subsequence of size 3 in C++
- Maximum Sum Increasing Subsequence using DP in C++ program
- Maximum Sum Increasing Subsequence\n
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program
- Maximum Sum Increasing Subsequence | DP-14 in C++
- Maximum product of subsequence of size k in C++
- Maximum product of a triplet (subsequence of size 3) in array in C++ Program.
- Java Program for Longest Increasing Subsequence
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++
- Program to find length of longest increasing subsequence in Python
- Longest Increasing Subsequence
- Maximum product subset of an array in C++ program
- Maximum product of a triplet (subsequence of size 3) in array in C++

Advertisements