Maximum product of an increasing subsequence
Last Updated :
28 Jul, 2022
Given an array of numbers, find the maximum product formed by multiplying numbers of an increasing subsequence of that array.
Note: A single number is supposed to be an increasing subsequence of size 1.
Examples:
Input : arr[] = { 3, 100, 4, 5, 150, 6 }
Output : 45000
Maximum product is 45000 formed by the
increasing subsequence 3, 100, 150. Note
that the longest increasing subsequence
is different {3, 4, 5, 6}
Input : arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }
Output : 21780000
Maximum product is 21780000 formed by the
increasing subsequence 10, 22, 33, 50, 60.
Prerequisite : Longest Increasing Subsequence
Approach: Use a dynamic approach to maintain a table mpis[]. The value of mpis[i] stores product maximum product increasing subsequence ending with arr[i]. Initially all the values of increasing subsequence table are initialized to arr[i]. We use recursive approach similar to LIS problem to find the result.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll lis(ll arr[], ll n)
{
ll mpis[n];
for ( int i = 0; i < n; i++)
mpis[i] = arr[i];
for ( int i = 1; i < n; i++)
for ( int j = 0; j < i; j++)
if (arr[i] > arr[j] && mpis[i] < (mpis[j] * arr[i]))
mpis[i] = mpis[j] * arr[i];
return *max_element(mpis, mpis + n);
}
int main()
{
ll arr[] = { 3, 100, 4, 5, 150, 6 };
ll n = sizeof (arr) / sizeof (arr[0]);
printf ( "%lld" , lis(arr, n));
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
class GFG {
static int lis( int [] arr, int n)
{
int [] mpis = new int [n];
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
mpis[i] = arr[i];
for ( int i = 1 ; i < n; i++)
for ( int j = 0 ; j < i; j++)
if (arr[i] > arr[j] && mpis[i]
< (mpis[j] * arr[i]))
mpis[i] = mpis[j] * arr[i];
for ( int k = 0 ; k < mpis.length; k++)
{
if (mpis[k] > max) {
max = mpis[k];
}
}
return max;
}
static public void main(String[] args)
{
int [] arr = { 3 , 100 , 4 , 5 , 150 , 6 };
int n = arr.length;
System.out.println(lis(arr, n));
}
}
|
Python3
import sys
def lis(arr, n):
mpis = []
Max = - sys.maxsize - 1
for i in range (n):
mpis.append(arr[i])
for i in range ( 1 ,n):
for j in range (i):
if (arr[i] > arr[j] and mpis[i] < (mpis[j] * arr[i])):
mpis[i] = mpis[j] * arr[i]
for k in range ( len (mpis)):
if (mpis[k] > Max ):
Max = mpis[k]
return Max
arr = [ 3 , 100 , 4 , 5 , 150 , 6 ]
n = len (arr)
print (lis(arr, n))
|
C#
using System;
using System.Linq;
public class GFG {
static long lis( long [] arr, long n)
{
long [] mpis = new long [n];
for ( int i = 0; i < n; i++)
mpis[i] = arr[i];
for ( int i = 1; i < n; i++)
for ( int j = 0; j < i; j++)
if (arr[i] > arr[j] && mpis[i] < (mpis[j] * arr[i]))
mpis[i] = mpis[j] * arr[i];
return mpis.Max();
}
static public void Main()
{
long [] arr = { 3, 100, 4, 5, 150, 6 };
long n = arr.Length;
Console.WriteLine(lis(arr, n));
}
}
|
PHP
<?PHP
function lis(& $arr , $n )
{
$mpis = array_fill (0, $n , NULL);
for ( $i = 0; $i < $n ; $i ++)
$mpis [ $i ] = $arr [ $i ];
for ( $i = 1; $i < $n ; $i ++)
for ( $j = 0; $j < $i ; $j ++)
if ( $arr [ $i ] > $arr [ $j ] && $mpis [ $i ] < ( $mpis [ $j ] * $arr [ $i ]))
$mpis [ $i ] = $mpis [ $j ] * $arr [ $i ];
return max( $mpis );
}
$arr = array ( 3, 100, 4, 5, 150, 6 );
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo lis( $arr , $n );
return 0;
?>
|
Javascript
<script>
of maximum product of an increasing
function lis(arr, n)
{
let mpis = [];
let max = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
mpis[i] = arr[i];
for (let i = 1; i < n; i++)
for (let j = 0; j < i; j++)
if (arr[i] > arr[j] && mpis[i]
< (mpis[j] * arr[i]))
mpis[i] = mpis[j] * arr[i];
for (let k = 0; k < mpis.length; k++)
{
if (mpis[k] > max)
{
max = mpis[k];
}
}
return max;
}
let arr = [ 3, 100, 4, 5, 150, 6 ];
let n = arr.length;
document.write(lis(arr, n));
</script>
|
Time Complexity: O(n^2)
Auxiliary Space : O(n)
Share your thoughts in the comments
Please Login to comment...