Find the Increasing subsequence of length three with maximum product
Last Updated :
18 Sep, 2023
Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order. Examples:
Input:
arr[] = {6, 7, 8, 1, 2, 3, 9, 10}
Output:
8 9 10
Input:
arr[] = {1, 5, 10, 8, 9}
Output: 5 8 9
Approach: Since we want to find the maximum product, we need to find following two things for every element in the given sequence:
- LSL: The largest smaller element on left of given element
- LGR: The largest greater element on right of given element.
Once we find LSL and LGR for an element, we can find the product of element with its LSL and LGR (if they both exist). We calculate this product for every element and return maximum of all products.
A simple method is to use nested loops. The outer loop traverses every element in sequence. Inside the outer loop, run two inner loops (one after other) to find LSL and LGR of current element. Time complexity of this method is O(n2). We can do this in O(nLogn) time.
For simplicity, let us first create two arrays LSL[] and LGR[] of size n each where n is number of elements in input array arr[]. The main task is to fill two arrays LSL[] and LGR[]. Once we have these two arrays filled, all we need to find maximum product LSL[i]*arr[i]*LGR[i] where 0 < i < n-1 (Note that LSL[i] doesn’t exist for i = 0 and LGR[i] doesn’t exist for i = n-1). We can fill LSL[] in O(nLogn) time. The idea is to use a Balanced Binary Search Tree like AVL. We start with empty AVL tree, insert the leftmost element in it. Then we traverse the input array starting from the second element to second last element. For every element currently being traversed, we find the floor of it in AVL tree.
If a floor exists, we store the floor in LSL[], otherwise we store NIL. After storing the floor, we insert the current element in the AVL tree. We can fill LGR[] in O(n) time. The idea is similar to this post. We traverse from right side and keep track of the largest element. If the largest element is greater than current element, we store it in LGR[], otherwise, we store NIL. Finally, we run a O(n) loop and return maximum of LSL[i]*arr[i]*LGR[i]. Note that we can avoid space required for LSL, we can find and use LSL values in final loop.
Implementation:
C++
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int binarySearch(vector< int > a, int x)
{
int left = 0;
int right = a.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (a[mid] >= x) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
if (left > 0) {
return left - 1;
}
else {
return -1;
}
}
int * countArray( int arr[], int n)
{
int LGR[n];
for ( int i = 0; i < n; i++) {
LGR[i] = arr[i];
}
LGR[n - 1] = -1;
int maxFromRight = arr[n - 1];
for ( int i = n - 2; i >= 0; i--) {
int temp = LGR[i];
LGR[i] = maxFromRight;
if (maxFromRight < temp) {
maxFromRight = temp;
}
}
int LSL[n];
LSL[0] = -1;
vector< int > lst;
lst.push_back(arr[0]);
for ( int i = 1; i < n; i++) {
int idx = binarySearch(lst, arr[i]);
if (idx != -1) {
LSL[i] = lst[idx];
}
lst.insert(lst.begin() + idx + 1, arr[i]);
}
int maxProduct = INT_MIN;
static int ans[] = { -1, -1, -1 };
for ( int i = 0; i < n; i++) {
int currP = LSL[i] * arr[i] * LGR[i];
if (currP > maxProduct && LSL[i] < arr[i]
&& arr[i] < LGR[i]) {
ans[0] = LSL[i];
ans[1] = arr[i];
ans[2] = LGR[i];
maxProduct = currP;
}
}
return ans;
}
int main()
{
int arr[] = { 6, 7, 8, 1, 2, 3, 9, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int * ans = countArray(arr, n);
if (ans[0] == -1) {
cout << "Not Present" << endl;
}
else {
for ( int i = 0; i < 3; i++) {
cout << ans[i] << " " ;
}
}
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class Solution {
public int binarySearch(List<Integer> a, int x)
{
int left = 0 ;
int right = a.size() - 1 ;
while (left <= right) {
int mid = (left + right) / 2 ;
if (a.get(mid) >= x) {
right = mid - 1 ;
}
else {
left = mid + 1 ;
}
}
if (left > 0 ) {
return left - 1 ;
}
else {
return - 1 ;
}
}
public int [] countArray( int [] arr, int n)
{
int [] LGR = new int [n];
for ( int i = 0 ; i < n; i++) {
LGR[i] = arr[i];
}
LGR[n - 1 ] = - 1 ;
int maxFromRight = arr[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--) {
int temp = LGR[i];
LGR[i] = maxFromRight;
if (maxFromRight < temp) {
maxFromRight = temp;
}
}
int [] LSL = new int [n];
LSL[ 0 ] = - 1 ;
List<Integer> lst = new ArrayList<>();
lst.add(arr[ 0 ]);
for ( int i = 1 ; i < n; i++) {
int idx = binarySearch(lst, arr[i]);
if (idx != - 1 ) {
LSL[i] = lst.get(idx);
}
lst.add(idx + 1 , arr[i]);
}
int maxProduct = Integer.MIN_VALUE;
int [] ans = new int [] { - 1 };
for ( int i = 0 ; i < n; i++) {
int currP = LSL[i] * arr[i] * LGR[i];
if (currP > maxProduct && LSL[i] < arr[i]
&& arr[i] < LGR[i]) {
ans = new int [] { LSL[i], arr[i], LGR[i] };
maxProduct = currP;
}
}
return ans;
}
public static void main(String[] args)
{
int [] ans = new Solution().countArray(
new int [] { 6 , 7 , 8 , 1 , 2 , 3 , 9 , 10 }, 8 );
if (ans[ 0 ] == - 1 ) {
System.out.println( "Not Present" );
}
else {
for ( int i = 0 ; i < ans.length; i++) {
System.out.print(ans[i] + " " );
}
}
}
}
|
Python3
from bisect import bisect_left
class Solution:
def BinarySearch( self ,a, x):
i = bisect_left(a, x)
if i:
return (i - 1 )
else :
return - 1
def countArray( self , arr, n) :
LGR = [i for i in arr]
LGR[ - 1 ] = - 1
max_from_right = arr[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
temp = LGR[i]
LGR[i] = max_from_right
if max_from_right< temp:
max_from_right = temp
LSL = [ 0 ] * (n)
LSL[ 0 ] = - 1
lst = []
lst.append(arr[ 0 ])
for i in range ( 1 , n):
idx = self .BinarySearch(lst, arr[i])
if (idx ! = - 1 ):
LSL[i] = lst[idx]
lst.insert(idx + 1 , arr[i])
maxProduct = float ( '-inf' )
ans = [ - 1 ]
for i in range ( 0 ,n):
currP = LSL[i] * arr[i] * LGR[i]
if currP>maxProduct and LSL[i]<arr[i] and arr[i]<LGR[i]:
ans = []
ans.extend([LSL[i],arr[i],LGR[i]])
maxProduct = currP
return ans
ans = Solution().countArray([ 6 , 7 , 8 , 1 , 2 , 3 , 9 , 10 ], 8 )
if (ans[ 0 ] = = - 1 ):
print ( "Not Present" )
else :
print ( * ans)
|
C#
using System;
using System.Collections.Generic;
class Solution
{
public int binarySearch(List< int > a, int x)
{
int left = 0;
int right = a.Count - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (a[mid] >= x)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
if (left > 0)
{
return left - 1;
}
else
{
return -1;
}
}
public int [] countArray( int [] arr, int n)
{
int [] LGR = new int [n];
for ( int i = 0; i < n; i++)
{
LGR[i] = arr[i];
}
LGR[n - 1] = -1;
int maxFromRight = arr[n - 1];
for ( int i = n - 2; i >= 0; i--)
{
int temp = LGR[i];
LGR[i] = maxFromRight;
if (maxFromRight < temp)
{
maxFromRight = temp;
}
}
int [] LSL = new int [n];
LSL[0] = -1;
List< int > lst = new List< int >();
lst.Add(arr[0]);
for ( int i = 1; i < n; i++)
{
int idx = binarySearch(lst, arr[i]);
if (idx != -1)
{
LSL[i] = lst[idx];
}
lst.Insert(idx + 1, arr[i]);
}
int maxProduct = int .MinValue;
int [] ans = new int [] { -1 };
for ( int i = 0; i < n; i++)
{
int currP = LSL[i] * arr[i] * LGR[i];
if (currP > maxProduct && LSL[i] < arr[i]
&& arr[i] < LGR[i])
{
ans = new int [] { LSL[i], arr[i], LGR[i] };
maxProduct = currP;
}
}
return ans;
}
public static void Main( string [] args)
{
int [] ans = new Solution().countArray(
new int [] { 6, 7, 8, 1, 2, 3, 9, 10 }, 8);
if (ans[0] == -1)
{
Console.WriteLine( "Not Present" );
}
else
{
for ( int i = 0; i < ans.Length; i++)
{
Console.Write(ans[i] + " " );
}
}
}
}
|
Javascript
function binarySearch(a, x) {
let left = 0;
let right = a.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (a[mid] >= x) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
if (left > 0) {
return left - 1;
}
else {
return -1;
}
}
function countArray(arr, n) {
let LGR = [];
for (let i = 0; i < n; i++) {
LGR[i] = arr[i];
}
LGR[n - 1] = -1;
let maxFromRight = arr[n - 1];
for (let i = n - 2; i >= 0; i--) {
let temp = LGR[i];
LGR[i] = maxFromRight;
if (maxFromRight < temp) {
maxFromRight = temp;
}
}
let LSL = [];
LSL[0] = -1;
let lst = [];
lst.push(arr[0]);
for (let i = 1; i < n; i++) {
let idx = binarySearch(lst, arr[i]);
if (idx != -1) {
LSL[i] = lst[idx];
}
lst.splice(idx + 1, 0, arr[i]);
}
let maxProduct = Number.MIN_SAFE_INTEGER;
let ans = [-1, -1, -1];
for (let i = 0; i < n; i++) {
let currP = LSL[i] * arr[i] * LGR[i];
if (currP > maxProduct && LSL[i] < arr[i]
&& arr[i] < LGR[i]) {
ans[0] = LSL[i];
ans[1] = arr[i];
ans[2] = LGR[i];
maxProduct = currP;
}
}
return ans;
}
let arr = [6, 7, 8, 1, 2, 3, 9, 10];
let n = arr.length;
let ans = countArray(arr, n);
if (ans[0] == -1) {
console.log( "Not Present" );
}
else {
for (let i = 0; i < 3; i++) {
console.log(ans[i] + " " );
}
}
|
Time Complexity:- O(nLogn)
Auxiliary Space:- O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...