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 necessary header files
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
// Function to perform binary search
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;
}
}
// Function to find LSL, LGR, and the maximum product of a
// triplet
int* countArray(int arr[], int n)
{
// Calculate LGR for each element
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;
}
}
// Calculate LSL for each element
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;
}
// Main function
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)
{
// Calculate LGR for each element
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;
}
}
// Calculate LSL for each element
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) :
# Calculate LGR for each element
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
# Calculate LSL for each element
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)
{
// Calculate LGR for each element
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;
}
}
// Calculate LSL for each element
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 to perform binary search
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 to find LSL, LGR, and the maximum product of a triplet
function countArray(arr, n) {
// Calculate LGR for each element
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;
}
}
// Calculate LSL for each element
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)
Similar Reads
Maximum product of an increasing subsequence
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
6 min read
Finding Maximum Length Subsequence with Product Conditions
Given an array A[] of length N, find the length of the longest subsequence S such that: If we took even number of elements from S, then the product of those elements should be a perfect square.If we took odd number of elements from S, then the product of those elements should not be a perfect square
11 min read
Maximum product of an increasing subsequence of size 3
Given an array of distinct positive integers, the task is to find the maximum product of increasing subsequence of size 3, i.e., we need to find arr[i]*arr[j]*arr[k] such that arr[i] < arr[j] < arr[k] and i < j < k < n Examples: Input: arr[] = {10, 11, 9, 5, 6, 1, 20}Output: 2200 Expl
14 min read
Maximum length Subsequence with Product less than given value for each query
Given an array of positive integers arr[] of length N and a query array query[] of length M, the task is to find the maximum length subsequence in the array whose product is not greater than query [i] for all the queries. Input: arr[] = {4, 5, 2, 1} queries[] = {3, 10, 21}Output: {2, 3, 3}Explanatio
8 min read
Maximize sum of product of Subsequence sum and its length
Given an array A[] of length N, the task is to find the maximum sum calculated by multiplying subsequence sum with its length and removing that from the given array until the array is empty. Examples: Input: N = 3, A[] = {2, -4, 3}Output: 6Explanation: The sub-sequences are chosen as:First sub-seque
8 min read
Find the Second Longest Increasing Subsequence
Given an array arr[] of integers, the task is to find the length of the second longest increasing subsequence (LIS). Examples: Input: arr[] = {1, 2, 3, 4}Output: 3 Explanation: Here the length of LIS is 4 which is whole array and length of second LIS is 3 which can be any sequence, {1, 2, 3} or {1,
15+ min read
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
Given an array arr[], the task is to find the maximum sum of all the elements which are not a part of the longest increasing subsequence. Examples: Input: arr[] = {4, 6, 1, 2, 3, 8} Output: 10 Explanation: Elements are 4 and 6 Input: arr[] = {5, 4, 3, 2, 1} Output: 14 Explanation: Elements are 5, 4,
8 min read
Maximum product of a triplet (subsequence of size 3) in array
Given an integer array, find a maximum product of a triplet in the array.Examples: Input: arr[ ] = [10, 3, 5, 6, 20]Output: 1200Explanation: Multiplication of 10, 6 and 20Input: arr[ ] = [-10, -3, -5, -6, -20]Output: -90Input: arr[ ] = [1, -4, 3, -6, 7, 0]Output: 168[Naive Approach] By Using three n
12 min read
Find the Substring with maximum product
Given string str containing only lowercase English alphabets of size N, the task is to find the substring having the maximum product. Each English alphabet has a value such that val('a') = 0, val('b') = 1, val('c') = 2, ......, val('z') = 25. Examples: Input: str = "sdtfakdhdahdzz" Output: hdzz Here
5 min read
Find heaviest increasing Subsequence with maximum sum in a String
Given a string s and an array arr[] representing the weights of each character in the string, the task is to find the heaviest increasing subsequence with the maximum sum and return that subsequence. Examples: Input: s = "acbde", arr[] = {2, 4, 3, 5, 1}Output: acdExplanation: The heaviest increasing
8 min read