Open In App

Find the Increasing subsequence of length three with maximum product

Last Updated : 18 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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] + " ");
    }
}


Output

8 9 10

Time Complexity:-  O(nLogn)
Auxiliary Space:- O(n)



Similar Reads

Count subsequence of length 4 having product of the first three elements equal to the fourth element
Given an array arr[] consisting of N positive integers, the task is to find the number of subsequences of length 4 having product of the first three elements equal to the fourth element. Examples: Input: arr[] = {10, 2, 2, 7, 40, 160}Output: 2Explanation:Following are the subsequences of length 4 satisfying the given criteria: {10, 2, 2, 40}, the p
7 min read
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 by the increasing subsequence 3, 100, 150. Note t
6 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] &lt; arr[j] &lt; arr[k] and i &lt; j &lt; k &lt; n Examples: Input: arr[] = {10, 11, 9, 5, 6, 1, 20}Output: 2200 Explanation: Increasing sub-sequences of size three ar
14 min read
Number of Permutations such that no Three Terms forms Increasing Subsequence
Given a number N. The task is to find the number of permutations of 1 to N such that no three terms of the permutation form an increasing subsequence. Examples: Input : N = 3 Output : 5 Valid permutations : 132, 213, 231, 312 and 321 and not 123 Input : N = 4 Output : 14 The above problem is an application of Catalan numbers. So, the task is to onl
5 min read
Maximum length subsequence such that adjacent elements in the subsequence have a common factor
Given an array arr[], the task is to find the maximum length of a subsequence such that the adjacent elements in the subsequence have a common factor. Examples: Input: arr[] = { 13, 2, 8, 6, 3, 1, 9 } Output: 5Max length subsequence with satisfied conditions: { 2, 8, 6, 3, 9 } Input: arr[] = { 12, 2, 8, 6, 3, 1, 9 } Output: 6Max length subsequence
9 min read
Longest Increasing Subsequence using Longest Common Subsequence Algorithm
Given an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.Examples: Input: arr[] = {12, 34, 1, 5, 40, 80} Output: 4 {12, 34, 40, 80} and {1, 5, 40, 80} are the longest increasing subsequences.Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80} Output: 6 Prerequisite: LCS, LISApproach: The longest increasing
12 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 subsequence with the maximum sum is "acd" as the
8 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}Explanation: For queries[0] : {2, 1} is maximum possible len
8 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.If there is no possible subsequence then return 0
11 min read
Count subsequence of length three in a given string
Given a string of length n and a subsequence of length 3. Find the total number of occurrences of the subsequence in this string. Examples : Input : string = "GFGFGYSYIOIWIN", subsequence = "GFG" Output : 4 Explanation : There are 4 such subsequences as shown: GFGFGYSYIOIWIN GFGFGYSYIOIWIN GFGFGYSYIOIWIN GFGFGYSYIOIWIN Input : string = "GFGGGZZYNOI
15 min read
Article Tags :
Practice Tags :